I am looking to join a number of ggplot figures using ggpubr
. However I am finding that the titles generated in ggplot
(using labs()
) overlap with the title generated in annotate_figure
(using fig.lab =
).
Here is a reproducible example:
#import libraries
library(ggplot2)
library(ggpubr)
#create data
data <- data.frame('type' = c('A', 'B', 'C'),
'value' = c(1, 2, 3))
#create figure
fig <- ggplot(data,
aes(x = type, y = value))
geom_point()
labs(title = 'My title')
theme(plot.title = element_text(hjust = 0.5))
#arrange figure (in my real case i would actually be sticking plots together)
arrange_fig <- ggarrange(fig)
#annotate plots
annotate_figure(arrange_fig,
fig.lab = 'My main title',
fig.lab.pos = 'top')
You can see the titles clearly overlapping.
I realize in my example, i don't need to center the ggplot title, but in my real case, I arrange three plots, each with a title and these then clash with the annotate_figure title
(fig.pos =
). This would be the case regardless of where I position the annotate_figure
title. Effectively I need to move it 'up'.
I can't find a solution online and I am amazed I have not had this issue previously.
CodePudding user response:
One option would be to switch to patchwork
:
library(ggplot2)
library(patchwork)
data <- data.frame(
"type" = c("A", "B", "C"),
"value" = c(1, 2, 3)
)
fig <- ggplot(
data,
aes(x = type, y = value)
)
geom_point()
labs(title = "My title")
theme(plot.title = element_text(hjust = 0.5))
fig plot_annotation(
title = "My main title",
theme = theme(
plot.title = element_text(hjust = 0.5)
)
)
fig fig fig plot_annotation(
title = "My main title",
theme = theme(
plot.title = element_text(hjust = 0.5)
)
)