问题描述
我对 annotate_figure
有问题。不幸的是,当我准备可重现的示例时,问题并未发生。我在一个长列中用 ggarrange
排列了 5 个图,我想要一个标题。但是,此标题未居中(将 just = "centre"
添加到 text_grob
命令没有帮助)。
当我在控制台中查看它时,它是居中的,所以我不知道我做错了什么。 代码如下:
Panelplot_Ring <- ggarrange(R1,R2,R3,R4,R5,ncol=1,nrow=5,common.legend = TRUE,legend="bottom")
Panelplot_Ring <- annotate_figure(Panelplot_Ring,left = text_grob("Ring width [mm]",rot = 90),top = text_grob("Tree-ring width",color = "black",face = "bold",size = 14))
emf("Tree_ring_levels.emf",width = 5/2.54*1.45,height = 20/2.54*1.45,emfplus=TRUE,emfplusFont=TRUE,emfplusRaster =TRUE)
Panelplot_Ring
dev.off()
data("ToothGrowth")
df <- ToothGrowth
df$dose <- as.factor(df$dose)
# Create some plots
# ::::::::::::::::::::::::::::::::::::::::::::::::::
# Box plot
bxp <- ggBoxplot(df,x = "dose",y = "len",color = "dose",palette = "jco")
# Dot plot
dp <- ggdotplot(df,palette = "jco")
# Density plot
dens <- ggdensity(df,x = "len",fill = "dose",palette = "jco")
# Arrange and annotate
# ::::::::::::::::::::::::::::::::::::::::::::::::::
figure <- ggarrange(bxp,dp,dens,ncol = 1,nrow = 3,legend = "bottom")
#> `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
figure <- annotate_figure(figure,top = text_grob("Visualizing Tooth Growth",color = "red",size = 14),left = text_grob("Test",color = "green",rot = 90))
emf("Test.emf",emfplusRaster =TRUE)
figure
dev.off()
我找不到代码中的差异
解决方法
有办法解决。您可以为最顶部的图添加标题。然后记得从 annotate_figure()
中删除那个。
data("ToothGrowth")
df <- ToothGrowth
df$dose <- as.factor(df$dose)
# Create some plots
# Plot on the top - here we make changes
bxp <- ggboxplot(df,x = "dose",y = "len",color = "dose",palette = "jco") + ggtitle("Your Title") +
theme(plot.title = element_text(hjust = 0.5,size=14,face="bold"))
dp <- ggdotplot(df,palette = "jco")
dens <- ggdensity(df,x = "len",fill = "dose",palette = "jco")
# Arrange and annotate
figure <- ggarrange(bxp,dp,dens,ncol = 1,nrow = 3,common.legend = TRUE,legend = "bottom")
figure <- annotate_figure(figure,left = text_grob("Ring width [mm]",rot = 90))
emf("Test.emf",width = 5/2.54*1.45,height = 20/2.54*1.45,emfPlus=TRUE,emfPlusFont=TRUE,emfPlusRaster =TRUE)
figure
dev.off()