wilkelab / cowplot

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

Interfere with`theme(legend.position="bottom")` #113

Closed AlexZHENGH closed 5 years ago

AlexZHENGH commented 5 years ago

I was trying to make the legend appear at the bottom of a figure, but cowplot seems to interfere with theme() in ggplot2 so the legend locates at the bottom-left corner (instead of the bottom-middle.

Here is a minimum example:

library(ggplot2)

ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)

p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) + 
  geom_boxplot()
p +  theme(legend.position="bottom")

image

library(cowplot)
p +  theme(legend.position="bottom")

image

I am using version 0.9.3 under R (ver. 3.5.0) and macOS Majove (ver. 10.14).

clauswilke commented 5 years ago

Just add legend.justification = "center" to the theme statement and it will work.