I have the following toy example:
library(ggplot2)
g <- ggplot(mpg, aes(class))
mpg$drv <- sample(as.character((-4:4)), size = length(mpg$drv), replace = T)
g + geom_bar(aes(fill = drv), position = "fill") + theme(legend.position = "bottom") + guides(colour = guide_legend(nrow = 1))
which for me yields the following figure.
Try as I might, I am unable to get the legend at the bottom to be in one row. I looked around and the guide_legend
appears to be having no effect. Is there a better way to do this, or is there another way to force the legend to be in one row?
You've specified colour
instead of fill
in guides
. Try this instead:
library(ggplot2)
mpg$drv <- sample(as.character((-4:4)), size = length(mpg$drv), replace = T)
g <- ggplot(mpg, aes(class))
g + geom_bar(aes(fill = drv), position = "fill") + theme(legend.position = "bottom") + guides(fill = guide_legend(nrow = 1))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With