2

I have this issue that a plot drawn with cowplot:::plot_grid cuts the legend of the left-hand plot just by a few mm's. Legend size is already at the absolute minimum of readability, and the white space between the two plots is OK (so its not margins i want to manipulate). However, even with justification="left", the legend is a bit bigger than the plot panel, and is then cut after

plot_grid(px, p2, align="h", nrow=1, rel_widths = c(1,0.675))

ggsave("plot.tiff", width=8.27, height=11.69/4)

enter image description here

There is still sufficient white space to the left. I know legends can be freely moved inside the plot, but is it possible to move a legend just a few centimeters from its justification anchor, if its drawn outside the plot?

This example does recreate the problem, and contains many of the arguments my real life example features (e.g. plotting the grid at two different widths), but i had to enlarge the font size of the legend, and the example does not have additional white space to the left of the legend.

bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) +
  geom_boxplot() + theme_bw() +
  theme(legend.text = element_text(size=20), # IRL the font size is much smaller
        axis.text.y=element_blank(),
        legend.key.size = unit(0.2, "cm"),
        legend.position = "bottom",
        legend.justification="left")+
  guides(fill=guide_legend(nrow=3)) +
  coord_flip() 
bp
bp1 <- bp + scale_fill_discrete("",labels=c("reallyreallyreallylongstring", 
                                         "evenlongerstring", 
                                         "youcannotbelievehowlongthisstringis!!11!"))


library(cowplot)
plot_grid(bp1, bp, align="h", nrow=1, rel_widths = c(1,0.675))
ggsave("test.tiff", width=8.27, height=11.69/4)

Currently, my workaround is to print single plots and manipulate it with illustrator, which is sth id like to avoid.

nouse
  • 3,315
  • 2
  • 29
  • 56
  • Does it help if you increase the margin of the plot? e.g. `bp + theme(legend.justification="left", plot.margin = grid::unit(c(1, 1, 1, 2), units = 'cm'))` – konvas May 09 '18 at 12:02
  • I was already playing with theme(plot.margin), but it somehow increases the relative legend size, so that even more of the legend gets cut. – nouse May 09 '18 at 12:07
  • Added a reproducible example, at which cowplot does cut the legend to the right, and i hope this can be solved by moving the legend a bit to the left. – nouse May 09 '18 at 12:14

2 Answers2

4

you can try

# get legend
legend_p1 <- get_legend(bp1)
legend_p2 <- get_legend(bp)

# remove legend
bp1_wl <- bp1 + theme(legend.position='none')
bp_wl <- bp + theme(legend.position='none')

# plot
plot_grid(plot_grid(bp1_wl, bp_wl, align="h", rel_widths = c(1,0.675)),
          plot_grid(legend_p1,legend_p2, rel_widths = c(1,0.675)), nrow=2, rel_heights = c(1,0.4))

enter image description here

Roman
  • 17,008
  • 3
  • 36
  • 49
  • 1
    @nouse try different paramters of the `legend.justification` as well. – Roman May 09 '18 at 12:36
  • That was a very good hint. New to me, one can go outside the box with negative values, e.g. c(-0.1,1) to avoid the get_legend calls. Although it wasnt enough for me, because anything larger than c(-0.3,1) was ignored :) – nouse May 09 '18 at 12:51
  • @Jimbou This helps, and for complicated legend arrangements it often makes sense to draw the legends separately, but the core of the issue is unnecessary white squares in `theme_bw()`. – Claus Wilke May 09 '18 at 16:01
1

This may look like a cowplot bug but it is not, and it would not have happened with the cowplot theme. The problem lies with theme_bw(): The second plot has a white background that is drawn on top of the first plot. If you remove the white background the legend can overlap from one plot into the other.

library(ggplot2)
bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) +
  geom_boxplot() + theme_bw() +
  theme(legend.text = element_text(size=20), # IRL the font size is much smaller
        axis.text.y=element_blank(),
        legend.key.size = unit(0.2, "cm"),
        legend.position = "bottom",
        legend.justification="left",
        # here we're removing plot background, legend background,
        # and legend box background, to be sure
        plot.background = element_blank(),
        legend.background = element_blank(),
        legend.box.background = element_blank())+
  guides(fill=guide_legend(nrow=3)) +
  coord_flip() 
bp
bp1 <- bp + scale_fill_discrete("",labels=c("reallyreallyreallylongstring", 
                                            "evenlongerstring", 
                                            "youcannotbelievehowlongthisstringis!!11!"))


library(cowplot)
plot_grid(bp1, bp, align="h", nrow=1, rel_widths = c(1,0.675))

enter image description here

(I'm currently running the development version of ggplot2 and I'll have to see why the legend runs out on the left of the plot, but that's a separate issue.)

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104