Different orientations for several legends in ggplot2?

How can I create a plot with two legends, where one legend is vertical and the other legend horizontal?

Using an aperture dataset, here is an example:

ggplot(iris,aes(x=Sepal.Width,y=Petal.Width,color=Species,size=Sepal.Length))+
geom_point() + 
scale_size_continuous(breaks=c(seq(from=5,to=7,by=0.4))) +
facet_wrap(~Species,ncol = 2) +
theme(legend.position=c(.7,.2))

I would like the legend of color to Speciesremain vertical, but above it there should be a horizontal inscription Sepal.Length. Is it possible?

Note. I understand that cutting makes color design unnecessary. I just use this as an example.

+4
source share
1 answer

You can control the features of certain legends using the interface guides.

ggplot(iris,aes(x=Sepal.Width,y=Petal.Width,color=Species,size=Sepal.Length))+
    geom_point() + 
    scale_size_continuous(breaks=c(seq(from=5,to=7,by=0.4))) +
    guides(size=guide_legend(direction='horizontal')) +
    facet_wrap(~Species,ncol = 2) +
    theme(legend.position=c(.7,.2))
+5
source

Source: https://habr.com/ru/post/1619994/


All Articles