【问题标题】:categorize with color and size in one ggplot legend在一个 ggplot 图例中按颜色和大小分类
【发布时间】:2015-02-23 08:19:12
【问题描述】:

我想绘制一些在 ggplot 中按大小和颜色分类的数据。例如:

require(ggplot2)

ggplot(mtcars, aes(wt, mpg)) + 
  geom_point(aes(size = qsec, color = qsec)) + 
  scale_colour_gradient(limits=c(15, 23), low = "blue", high = "red")

我将如何改变它以使颜色和大小都显示在同一个图例中?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    首先,您需要在scale_color_gradient 中指定guide = 'legend'(默认为"colourbar")。当然,您需要对colorsize 刻度使用相同的限制。

    ggplot(mtcars, aes(wt, mpg)) + 
      geom_point(aes(size = qsec, color = qsec)) + 
      scale_colour_gradient(limits=c(15, 23), low = "blue", high = "red", guide = 'legend') +
      scale_size_continuous(limits=c(15, 23))
    

    【讨论】:

      猜你喜欢
      • 2023-02-17
      • 2022-11-25
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      • 2013-09-02
      • 2019-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多