【问题标题】:annotate_figure does not center the titleannotate_figure 不使标题居中
【发布时间】:2021-07-21 07:40:41
【问题描述】:

annotate_figure 有问题。不幸的是,当我准备可重现的示例时,问题并没有发生。我在一长列中安排了 5 个带有ggarrange 的图,我想要一个标题。但是,此标题未居中(将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, 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 = 1, nrow = 3, common.legend = TRUE, legend = "bottom")
#> `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
figure <- annotate_figure(figure, top = text_grob("Visualizing Tooth Growth", color = "red", face = "bold", size = 14), left = text_grob("Test", color = "green", 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()

我找不到代码中的差异

【问题讨论】:

  • 不,我更困惑。当我将其导出为 jpeg 时,一切正常。但在示例代码中,它也适用于 emf。

标签: r ggplot2 annotate


【解决方案1】:

有办法解决。您可以在最顶部的绘图中添加标题。然后记得从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, x = "dose", y = "len", color = "dose", 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()

【讨论】:

  • 感谢您的建议。但是如果我这样做,第一个图会变得比其他图小。出于这个原因,我使用了 annotate_figure() 命令。
猜你喜欢
  • 2022-10-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-31
  • 2017-02-23
  • 2016-01-24
  • 2019-08-05
  • 2023-03-28
  • 2023-03-14
相关资源
最近更新 更多