wilkelab / cowplot

cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
https://wilkelab.org/cowplot/
704 stars 84 forks source link

key height gets stretched when using multiline key-labels in the legend with cowplot #152

Closed mluerig closed 4 years ago

mluerig commented 4 years ago

I have discovered some what I think undesireable behavior when using theme_cowplot() with ggplot: the key height gets stretched when using multiline key-labels in the legend. this is not affected my manipulations of the key height in theme() or guides()

require(ggplot2)
require(cowplot)
set.seed(15)

df <- data.frame(x=1:50, y=rnorm(50, 10, 2), var=rep(c("A","B","C","D","E"),10))
labs = c("A oneline","B oneline","C oneline", "D\ntwolines","E\ntwolines")
cols = c('#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00')

ggplot(df) + theme_cowplot() + # with or without theme_cowplot() changes the key height
    geom_ribbon(aes(x=x, ymin=y-1, ymax=y+1, fill=var), alpha=0.3) +
    geom_line(aes(x=x, y=y, colour=var), alpha=0.3, size=2) +
    scale_fill_manual(values=cols, labels = labs) +
    theme(legend.position="bottom",
          legend.title = element_blank()) +
    guides(fill=guide_legend(ncol=3, byrow=TRUE))

I think this should be at least adjustable - or am I missing something?

clauswilke commented 4 years ago

Please run your example through the reprex package so I can see the output. And if you think this is a cowplot bug I'd like to see that the behavior is different without cowplot. Otherwise it's a ggplot2 bug, or maybe no bug at all.

mluerig commented 4 years ago

here you go

require(ggplot2)
#> Loading required package: ggplot2
require(cowplot)
#> Loading required package: cowplot
#> 
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#>   default ggplot2 theme anymore. To recover the previous
#>   behavior, execute:
#>   theme_set(theme_cowplot())
#> ********************************************************
require(reprex)
#> Loading required package: reprex

set.seed(15)

df <- data.frame(x=1:50, y=rnorm(50, 10, 2), var=rep(c("A","B","C","D","E"),10))
labs = c("A oneline","B oneline","C oneline", "D\ntwolines","E\ntwolines")
cols = c('#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00')

ggplot(df) + 
    geom_ribbon(aes(x=x, ymin=y-1, ymax=y+1, fill=var), alpha=0.3) +
    geom_line(aes(x=x, y=y, colour=var), alpha=0.3, size=2) +
    scale_fill_manual(values=cols, labels = labs) +
    scale_colour_manual(values=cols, labels = labs) +
    theme(legend.position="bottom",
          legend.title = element_blank()) +
    guides(fill=guide_legend(ncol=3, byrow=TRUE))


ggplot(df) + theme_cowplot() +
    geom_ribbon(aes(x=x, ymin=y-1, ymax=y+1, fill=var), alpha=0.3) +
    geom_line(aes(x=x, y=y, colour=var), alpha=0.3, size=2) +
    scale_fill_manual(values=cols, labels = labs) +
    scale_colour_manual(values=cols, labels = labs) +
    theme(legend.position="bottom",
          legend.title = element_blank()) +
    guides(fill=guide_legend(ncol=3, byrow=TRUE))

clauswilke commented 4 years ago

This is a ggplot2 bug that just happens to be triggered by the larger font size that the cowplot theme uses. Please file an issue directly with ggplot2.

require(ggplot2)
#> Loading required package: ggplot2

set.seed(15)

df <- data.frame(x=1:50, y=rnorm(50, 10, 2), var=rep(c("A","B","C","D","E"),10))
labs = c("A oneline","B oneline","C oneline", "D\ntwolines","E\ntwolines")
cols = c('#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00')

ggplot(df) + 
  geom_ribbon(aes(x=x, ymin=y-1, ymax=y+1, fill=var), alpha=0.3) +
  geom_line(aes(x=x, y=y, colour=var), alpha=0.3, size=2) +
  scale_fill_manual(values=cols, labels = labs) +
  theme(legend.position="bottom",
        legend.title = element_blank(),
        legend.text = element_text(size = 14)) +
  guides(fill=guide_legend(ncol=3, byrow=TRUE))

Created on 2019-12-13 by the reprex package (v0.3.0)

mluerig commented 4 years ago

I see - thanks!