大家好,我正在使用一個小資料框在ggplot2. 我的資料框是df,我dput()在最后包含它。我有情節,當我使用patchwork. 我想要沒有空格的最終圖,以便中間的線(即軸)可以將圖連接在一起。這是代碼:
library(ggplot2)
library(patchwork)
library(cowplot)
library(ggtext)
#Plot 1
G1 <- ggplot(df,aes(x=Var1,y=Var2))
geom_line(aes(color=Group,group=Group),size=1)
geom_point(aes(color=Group,group=Group,shape=Group),size=2)
scale_y_continuous(limits = c(0,NA),
sec.axis = dup_axis(name = '',breaks = NULL,labels = NULL))
theme_half_open(12)
background_grid()
theme(
strip.background = element_blank(),
strip.text = element_textbox(
size = 12,
face = 'bold',
color = "white", fill = "#5D729D", box.color = "#4A618C",
halign = 0.5, linetype = 1, r = unit(5, "pt"), width = unit(1, "npc"),
padding = margin(2, 0, 1, 0), margin = margin(3, 3, 3, 3)
)
)
theme(legend.position = 'top',
axis.title = element_text(color='black',face='bold'),
axis.text = element_text(color='black',face='bold'),
legend.text = element_text(color='black',face='bold'),
legend.title = element_text(color='black',face='bold'),
panel.grid = element_blank(),
legend.justification = 'center',
plot.margin = unit(c(0,0,0,0), "cm"),
plot.title = element_text(color='black',
size=12,
face='bold',hjust=0.5),
plot.caption = element_text(face='bold'))
#Plot 2
G2 <- ggplot(df,aes(x=Var1,y=Var3))
geom_line(aes(color=Group,group=Group),size=1)
geom_point(aes(color=Group,group=Group,shape=Group),size=2)
scale_y_continuous(limits = c(0,NA),position = 'right')
theme_half_open(12)
background_grid()
theme(
strip.background = element_blank(),
strip.text = element_textbox(
size = 12,
face = 'bold',
color = "white", fill = "#5D729D", box.color = "#4A618C",
halign = 0.5, linetype = 1, r = unit(5, "pt"), width = unit(1, "npc"),
padding = margin(2, 0, 1, 0), margin = margin(3, 3, 3, 3)
)
)
theme(legend.position = 'top',
axis.title = element_text(color='black',face='bold'),
axis.text = element_text(color='black',face='bold'),
legend.text = element_text(color='black',face='bold'),
legend.title = element_text(color='black',face='bold'),
panel.grid = element_blank(),
legend.justification = 'center',
plot.margin = unit(c(0,0,0,0), "cm"),
plot.title = element_text(color='black',
size=12,
face='bold',hjust=0.5),
plot.caption = element_text(face='bold'))
#Merge plots
G3 <- G1 G2 plot_layout(guides = 'collect')&theme(legend.position = 'top')
這是結果G3:

可以看出,由于中間線和第二個地塊之間存在空間,因此地塊沒有完全連接。我怎樣才能洗掉那個空間?

非常感謝。資料df如下:
#Data
df <- structure(list(Var1 = c("A", "B", "C", "A", "B", "C"), Group = c("G1",
"G1", "G1", "G2", "G2", "G2"), Var2 = c(1L, 3L, 4L, 8L, 9L, 0L
), Var3 = c(9L, 8L, 4L, 5L, 1L, 1L)), class = "data.frame", row.names = c(NA,
-6L))
uj5u.com熱心網友回復:
你可以這樣做,你可以調整 plot_layout(widths = c())或者你可以調整邊距,& theme(plot.margin = ...)但是,我認為plot.margin在這種情況下不起作用。
要實施widths到您的繪圖中,您需要添加一個間隔圖并用于widths調整間隔,以便這些圖完全連接在一起
G3 <- G1 plot_spacer() G2 plot_layout(widths = c(4, -1.1 ,4.5),guides = "collect")& theme(legend.position = "top")
這里 plot1 的寬度為 4,間隔圖的寬度為 -1.1,允許您將這些圖連接在一起,plot2 的寬度為 4.5。我不確定為什么 plot2 需要具有比 plot1 更大的寬度,但是當它們的寬度都設定為 4 時,這兩個圖看起來不正確。

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376450.html
