Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Labels do not align correctly in 12 panel figure using Cowplot - plot_grid #32

Closed
czq2007hlj opened this issue Mar 18, 2016 · 3 comments

Comments

@czq2007hlj
Copy link

I used plot_grid to generate a 12 (3 row by 4 col) panel figure, however, I was not able to get the alignment correct for the 3rd row after "hjust = -6".
Thank you for your time and help!

PP1 <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + geom_point(size = 2.5)
....

PP12 <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + geom_point(size = 2.5)

plot3by4 <- plot_grid(PP1, PP2, PP3, PP4, PP5, PP6, PP7, PP8, PP9, PP10, PP11, PP12, labels=c("A", "B", "C", "D","E", "F", "G", "H", "I", "J", "K", "L"), ncol = 4, nrow = 3, align = 'v', hjust=-6, label_size=17)

save_plot("plot3by4.png", plot3by4, ncol = 4, nrow = 3, base_aspect_ratio = 1 )

@clauswilke
Copy link
Contributor

Apparently this is caused by the fact that the label font is proportional width. The effect of hjust depends on the width of the label, and that width varies. In particular, the label "I" is very narrow, so the effect is biggest there.

You can see that if all labels are the same, then there is no problem:

plot3by4 <- plot_grid(PP1, PP2, PP3, PP4, PP5, PP6, PP7, PP8, PP9, PP10, PP11, PP12,
                      labels=c("A", "A", "A", "A","A", "A", "A", "A", "A", "A", "A", "A"),
                      ncol = 4, nrow = 3, align = 'v', hjust=-6, label_size=17)
plot3by4

rplot02

Of course that won't help you much. One solution would be to provide individual hjust values for each label, by making hjust a vector of length 12. That is possible but ugly. Alternatively, I would recommend adding the labels manually, like so:

xoff <- .35 # relative x position of label, within one plot
yoff <- .93 # relative y position of label, within one plot
plot3by4 <- plot_grid(PP1, PP2, PP3, PP4, PP5, PP6, PP7, PP8, PP9, PP10, PP11, PP12,
                      ncol = 4, nrow = 3, align = 'v') +
            draw_plot_label(label=c("A", "B", "C", "D","E", "F", "G", "H", "I", "J", "K", "L"),
                            x=rep((xoff+0:3)/4, 3),
                            y=1-(1-yoff+rep(0:2, each=4))/3,
                            hjust=.5, vjust=.5, size=17)
plot3by4

rplot01

Thanks for pointing this out. I wasn't aware of this issue, and I may have to modify the way label placement is done in plot_grid.

@czq2007hlj
Copy link
Author

Perfect.
Thank you so much for such a prompt reply!

@clauswilke
Copy link
Contributor

This problem is now permanently solved in cowplot devel through the addition of the label_x and label_y arguments to plot_grid():

plot3by4 <- plot_grid(PP1, PP2, PP3, PP4, PP5, PP6, PP7, PP8, PP9, PP10, PP11, PP12,
     labels = "AUTO", ncol = 4, nrow = 3, label_x = .3, hjust = 0, label_size=17)

screen shot 2017-07-28 at 9 03 43 am

flying-sheep pushed a commit to flying-sheep/cowplot that referenced this issue Aug 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants