0

I have a problem with annotate_figure. Unfortunately the problem does not occure when I prepared my reproducible example. I arranged 5 plots with ggarrange in one long column and I would like to have a title. However, this title is not centered (add just = "centre" to the text_grob command does not help). enter image description here

When I look at it in the console it is centered so I do not know what I make wrong. Here is the code:

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()

Here an example code:

    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()

I can not find the differences in the codes

stefan
  • 90,330
  • 6
  • 25
  • 51
T.Tree
  • 91
  • 5
  • No I am even more confused. When I export it as jpeg everything works well. But in the example code it also works with emf. – T.Tree Jul 21 '21 at 07:42

1 Answers1

0

There is a way around. You can add a title to the plot you have at the very top. Then remember to remove the one from 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()

enter image description here

KacZdr
  • 1,267
  • 3
  • 8
  • 23
  • Thank you for the suggestion. But if I do it in this way, the first graph becomes smaller than the other graphs. For that reason I used annotate_figure() command. – T.Tree Jul 21 '21 at 09:18