【问题标题】:ggplot: get discrete legend with scale_fill_gradientggplot:使用 scale_fill_gradient 获取离散图例
【发布时间】:2018-06-06 11:54:48
【问题描述】:

我正在尝试从“R Graphics Cookbook”第 87 页重新创建一个示例图。

我正在尝试使用以下代码重新创建,但没有得到结果:

library(ggplot2)
library(hexbin)

sp <- ggplot(diamonds, aes(x=carat, y=price))
sp + stat_binhex() +
  scale_fill_gradient(low="lightblue", high="red",
                      breaks=c(0, 250, 500, 1000, 2000, 4000, 6000),
                      limits=c(0, 6000)) 

这给了

【问题讨论】:

  • 没有得到结果是什么意思?
  • 添加guide="legend"(你可能还想转换trans="log"
  • @PoGibas,我几乎到了那里,但传说不匹配。这是我的:imgur.com/8l2543P
  • @balkon16 ;您能否将解决方案添加为答案,而不是在您的问题中。谢谢
  • 不要编辑您的问题标题以包含“已解决”。相反,发布答案,并通过单击旁边的复选标记“接受”它。 (您必须等待 24 小时才能接受自己的答案。)

标签: r ggplot2 gradient legend


【解决方案1】:

问题解决了。原来我必须添加指南参数:

sp <- ggplot(diamonds, aes(x=carat, y=price))
    sp + stat_binhex() +
      scale_fill_gradient(low="lightblue", high="red",
                          breaks=c(0, 250, 500, 1000, 2000, 4000, 6000),
                          limits=c(0, 6000), guide = "legend") 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 2021-11-01
    • 2021-03-20
    相关资源
    最近更新 更多