我正在尝试注释使用ggarrange()
包中的ggpubr
排列的地块网格。为此,在生成绘图后,我使用了annotate_figure()
函数。
我的问题:以交互方式进行操作(即不使用绘图创建文件)时,它可以正常运行,但是当我导出文件(使用ggexport()
)时,未显示注释。
示例: 参见example given in the documentation
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, x = "dose", y = "len", color = "dose", palette = "jco")
# Density plot
dens <- ggdensity(df, x = "len", fill = "dose", palette = "jco")
# Arrange and annotate
# ::::::::::::::::::::::::::::::::::::::::::::::::::
figure <- ggarrange(bxp, dp, dens, ncol = 2, nrow = 2)
#> `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
annotate_figure(figure,
top = text_grob("Visualizing Tooth Growth", color = "red", face = "bold", size = 14),
bottom = text_grob("Data source: \n ToothGrowth data set", color = "blue",
hjust = 1, x = 1, face = "italic", size = 10),
left = text_grob("Figure arranged using ggpubr", color = "green", rot = 90),
right = "I'm done, thanks :-)!",
fig.lab = "Figure 1", fig.lab.face = "bold"
)
这很好用。但是,如果我添加ggexport(figure, "whatever.pdf")
,则创建的文件将不包含注释。
任何想法如何解决这个问题?
答案 0 :(得分:0)
您只需要将annotate_figure(...)分配给变量即可显示或保存,如注释中所述。
这里是将变量分配回变量本身的答案:
figure <- ggarrange(bxp, dp, dens, ncol = 2, nrow = 2)
figure <- annotate_figure(figure,
top = text_grob("Visualizing Tooth Growth", color = "red", face = "bold", size = 14),
bottom = text_grob("Data source: \n ToothGrowth data set", color = "blue",
hjust = 1, x = 1, face = "italic", size = 10),
left = text_grob("Figure arranged using ggpubr", color = "green", rot = 90),
right = "I'm done, thanks :-)!",
fig.lab = "Figure 1", fig.lab.face = "bold"
)
ggsave(filename="figure.png", plot = figure)
ggexport(figure, filename = "figure2.png")