在绘图 ggplot2 之间减少 space
Reduce space between plots ggplot2
我正在使用 bookdown
和 emmeans
包。我正在尝试渲染一个 pdf,我可以在其中放置一系列相互之间最小 space 的情节
```{r 3AeMixEffCmass,out.width = c("32%","32%","32%"), fig.align = "center", fig.pos="!h",fig.show = 'hold',fig.cap='(ref:3AeMixEffCmass)', message=FALSE, warning=FALSE, echo=FALSE}
### plot letter identifier:
library(grid)
my_grob = grobTree(textGrob("A1", x=0.85, y=0.95, hjust=0, gp=gpar(col="black", fontsize=15, fontface="italic")))
library(ggplot2)
### Order the levels for printing:
cld.mixed_C_L$managem = factor(cld.mixed_C_L$managem, levels=c("WTH", "CH", "CHF"))
### reset row number:
cld.mixed_C_L<- with(cld.mixed_C_L, cld.mixed_C_L[order(managem),])
rownames(cld.mixed_C_L) <- NULL
cld.mixed_C_L
看起来像这样:
cld.mixed_C_L <- structure(list(managem = structure(c(1L, 3L, 2L), .Label = c("CH",
+ "CHF", "WTH"), class = "factor"), response = c(2.46142878854869,
+ 2.47526369032351, 3.25797765207699), SE = c(0.111893590886356,
+ 0.112522508873481, 0.148103743733873), df = c(5.22163854368496,
+ 5.22163854368496, 5.22163854368496), lower.CL = c(2.10382917999276,
+ 2.11565412905959, 2.78465437801484), upper.CL = c(2.87981160196529,
+ 2.89599809934785, 3.81175432945472), .group = c("a", "a", "b"
+ )), row.names = c(1L, 3L, 2L), class = "data.frame")
创建情节:
plot.mixed.lme<-ggplot(cld.mixed_C_L,aes(x = managem,y=response,color= managem, label=.group))+
theme_bw()+
geom_point(shape = 15, size = 4) +
geom_errorbar(aes(ymin = lower.CL,ymax = upper.CL),width = 0.2,size = 0.7)+
theme(aspect.ratio = 2/1) +
#### following lines adds legend inside the boxplot:
theme(legend.justification=c(1,0), legend.position=c(1,0),
legend.direction="vertical",
legend.box="vertical",
legend.box.just = c("top"),
legend.background = element_rect(fill=alpha('white', 0.4)))+
#removes legend:
guides(fill = FALSE, color= FALSE) +
#### remove axis labels:
theme(plot.title = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank()) +
#### move significance letters:
geom_text(nudge_x = c(0.3, 0.3, 0.3),
nudge_y = c(0, 0,0),
color = "black") +
#### set colours of the boxes:
scale_color_manual(values = c("#333333", "#333333", "#333333"))+
annotation_custom(my_grob)
### print three plots as example:
plot.mixed.lme
plot.mixed.lme
plot.mixed.lme
最终 pdf 文档的结果如下所示:
我一直在想办法减少地块之间的 space,但到目前为止我还没有成功。感谢任何帮助。
非常感谢。
地块大小不匹配的问题
(见评论)
从块选项中删除 out.width = c("32%","32%","32%")
并使用 cowplot
的 plot_grid
函数,如下所示:
cowplot::plot_grid(plot.mixed.lme, plot.mixed.lme, plot.mixed.lme, nrow = 1)
我正在使用 bookdown
和 emmeans
包。我正在尝试渲染一个 pdf,我可以在其中放置一系列相互之间最小 space 的情节
```{r 3AeMixEffCmass,out.width = c("32%","32%","32%"), fig.align = "center", fig.pos="!h",fig.show = 'hold',fig.cap='(ref:3AeMixEffCmass)', message=FALSE, warning=FALSE, echo=FALSE}
### plot letter identifier:
library(grid)
my_grob = grobTree(textGrob("A1", x=0.85, y=0.95, hjust=0, gp=gpar(col="black", fontsize=15, fontface="italic")))
library(ggplot2)
### Order the levels for printing:
cld.mixed_C_L$managem = factor(cld.mixed_C_L$managem, levels=c("WTH", "CH", "CHF"))
### reset row number:
cld.mixed_C_L<- with(cld.mixed_C_L, cld.mixed_C_L[order(managem),])
rownames(cld.mixed_C_L) <- NULL
cld.mixed_C_L
看起来像这样:
cld.mixed_C_L <- structure(list(managem = structure(c(1L, 3L, 2L), .Label = c("CH",
+ "CHF", "WTH"), class = "factor"), response = c(2.46142878854869,
+ 2.47526369032351, 3.25797765207699), SE = c(0.111893590886356,
+ 0.112522508873481, 0.148103743733873), df = c(5.22163854368496,
+ 5.22163854368496, 5.22163854368496), lower.CL = c(2.10382917999276,
+ 2.11565412905959, 2.78465437801484), upper.CL = c(2.87981160196529,
+ 2.89599809934785, 3.81175432945472), .group = c("a", "a", "b"
+ )), row.names = c(1L, 3L, 2L), class = "data.frame")
创建情节:
plot.mixed.lme<-ggplot(cld.mixed_C_L,aes(x = managem,y=response,color= managem, label=.group))+
theme_bw()+
geom_point(shape = 15, size = 4) +
geom_errorbar(aes(ymin = lower.CL,ymax = upper.CL),width = 0.2,size = 0.7)+
theme(aspect.ratio = 2/1) +
#### following lines adds legend inside the boxplot:
theme(legend.justification=c(1,0), legend.position=c(1,0),
legend.direction="vertical",
legend.box="vertical",
legend.box.just = c("top"),
legend.background = element_rect(fill=alpha('white', 0.4)))+
#removes legend:
guides(fill = FALSE, color= FALSE) +
#### remove axis labels:
theme(plot.title = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank()) +
#### move significance letters:
geom_text(nudge_x = c(0.3, 0.3, 0.3),
nudge_y = c(0, 0,0),
color = "black") +
#### set colours of the boxes:
scale_color_manual(values = c("#333333", "#333333", "#333333"))+
annotation_custom(my_grob)
### print three plots as example:
plot.mixed.lme
plot.mixed.lme
plot.mixed.lme
最终 pdf 文档的结果如下所示:
我一直在想办法减少地块之间的 space,但到目前为止我还没有成功。感谢任何帮助。
非常感谢。
地块大小不匹配的问题
(见评论)
从块选项中删除 out.width = c("32%","32%","32%")
并使用 cowplot
的 plot_grid
函数,如下所示:
cowplot::plot_grid(plot.mixed.lme, plot.mixed.lme, plot.mixed.lme, nrow = 1)