Legend on bottom, two rows wrapped in ggplot2 in r

前端 未结 2 1112
终归单人心
终归单人心 2020-12-07 22:24
Rdates <- c(\"2007-01-31\",\"2007-02-28\",\"2007-03-30\",\"2007-04-30\",\"2007-05-31\",\"2007-06-29\",\"2007-07-31\",\"2007-08-31\",\"2007-09-28\",\"2007-10-31\")         


        
相关标签:
2条回答
  • 2020-12-07 22:54

    You were really close. Try this at the very end:

    gg+guides(fill=guide_legend(nrow=2,byrow=TRUE))
    

    0 讨论(0)
  • 2020-12-07 23:08

    The solution above is presented for a single aesthetic. In some cases, you may want to wrap the legend into rows instead of columns across different aesthetics. For posterity, this is shown below.

    library(ggplot2)
    
    ggplot(diamonds, aes(x=carat, y=price, col=clarity, shape=cut)) +
      geom_point() +
      theme(legend.position="bottom")
    

    The legend is cut off below:

    To wrap the legend using rows, we specify legend.box="vertical". Below, we also reduce the margin for compactness.

    ggplot(diamonds, aes(x=carat, y=price, col=clarity, shape=cut)) +
      geom_point() +
      theme(legend.position="bottom", legend.box="vertical", legend.margin=margin())
    

    0 讨论(0)
提交回复
热议问题