r-ggplot2中多个图例的不同方向

  • 本文关键字:同方向 r-ggplot2 r ggplot2
  • 更新时间 :
  • 英文 :


如何生成具有两个图例的图,其中一个图例是垂直的,另一个图例为水平的?

使用虹膜数据集,这里有一个例子:

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))

我希望Species的颜色图例保持垂直,但Sepal.Length的图例在其下方保持水平。这可能吗?

注意:我知道贴面会使颜色图例变得不必要。我只是举个例子。

您可以使用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))

最新更新