Home > Back-end >  GGplot2 in combination with Guide_legend arguments
GGplot2 in combination with Guide_legend arguments

Time:05-23

theme_pubr in the ggpubr package by default creates legends with the title on the side and the variables distributed horizontally. However,i'm trying to make a legend with the title on top and the species in one column.

I have tried using guides(colour=guide_legend(ncol=1)) and guides(colour = guide_legend(title.position = "top")) in combination, however one seems to override the other. Can anybody explain why this happens and how to work around it?

example code using the iris dataset.

library(ggplot2)
library(datasets)
library(ggpubr)



ggplot(iris,aes(Species, Petal.Length)) 
  geom_point(aes(colour= Species)) 
  theme_pubr() 
  labs_pubr()  
  guides(colour=guide_legend(ncol=1)) 
  guides(colour = guide_legend(title.position = "top"))

Thanks in advance,

Stuart

CodePudding user response:

Instead of creating two functions that overwrite each other, you can use both arguments in one function

guides(colour=guide_legend(title.position = "top", ncol=1))
  • Related