【问题标题】:How to hide 1 of 2 legends in ggplot2 geom_hex如何在 ggplot2 geom_hex 中隐藏 2 个图例中的 1 个
【发布时间】:2022-01-14 14:22:58
【问题描述】:

我有这个 geom_hex 图,它同时定义了填充颜色和轮廓颜色。我想消除轮廓的图例(上面写着“计数”)并保留填充图例(上面写着“频率”)。下面的链接显示了情节的图像。

plotX <- ggplot(data = rt28, aes(x = TimeNumericValue, y = load)) +
  geom_hex(aes(colour = ..count..))
plotX + labs(
  title = 'Route 28 Bus Ridership',
  subtitle = 'Fall 2016 - Fall 2019',
  x = 'Time of Day',
  y = 'Average Passenger Load',
  fill = 'Freq') +
  theme_minimal()

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    至少有两个选项可以达到您想要的结果。使用+ guides(color = "none") 删除颜色图例,或使用+ labs(..., color = "Freq") 为颜色图例使用与填充相同的名称,以便合并两个图例。

    使用ggplot2::diamonds 数据集作为示例数据:

    library(ggplot2)
    
    d <- ggplot(diamonds, aes(carat, price)) + 
      geom_hex(aes(colour = ..count..)) + 
      labs(
      title = 'Route 28 Bus Ridership',
      subtitle = 'Fall 2016 - Fall 2019',
      x = 'Time of Day',
      y = 'Average Passenger Load',
      fill = 'Freq') +
      theme_minimal()
    
    d + labs(color = "Freq")
    

    d + guides(color = "none")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-17
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      相关资源
      最近更新 更多