I'm using ggplot2 to create some figures with titles, but finding that when titles have a descender (eg, lowercase p, q, g, y) the actual size of the plot shrinks slightly to accommodate the larger space needed by the title.
Are there ways within normal ggplot functionality to fix the plot size so that figures are in 100% consistent position regardless of title?
Here's some quick sample code that shows the issue; folks might need to run code locally to clearly see the differences in the images.
library(ggplot2)
# No letters with descenders in title
ggplot(data=mtcars,aes(x=disp,y=mpg)) +
geom_point() + ggtitle("Scatter Plot")
# Title has a descender (lowercase 'p')
ggplot(data=mtcars,aes(x=disp,y=mpg)) +
geom_point() + ggtitle("Scatter plot")
you can set the relevant height in the gtable,
library(ggplot2)
p1 <- ggplot() + ggtitle("a")
p2 <- ggplot() + ggtitle("a\nb")
gl <- lapply(list(p1,p2), ggplotGrob)
th <- do.call(grid::unit.pmax, lapply(gl, function(g) g$heights[3]))
gl <- lapply(gl, function(g) {g$heights[3] <- th; g})
gridExtra::grid.arrange(grobs = gl, nrow=1)
Edit: here's how to edit one plot for simplicity
g = ggplotGrob(qplot(1,1) + ggtitle('title'))
g$heights[3] = grid::unit(3,"line")
grid.draw(g)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.