gpt4 book ai didi

r - ggplot/ggpubr : annotate_figure ignored when exporting plot

转载 作者:行者123 更新时间:2023-12-03 23:53:48 25 4
gpt4 key购买 nike

我正在尝试注释我使用 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") ,创建的文件将不包含注释。

知道如何解决这个问题吗?

最佳答案

您只需要将 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")

arranged figure

关于r - ggplot/ggpubr : annotate_figure ignored when exporting plot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53066825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com