【问题标题】:ggplot2 histogram scale_fill_gradient not working on continuous variableggplot2直方图scale_fill_gradient不适用于连续变量
【发布时间】:2021-04-01 02:14:18
【问题描述】:

我无法使用 ggplot2 直方图让连续变量在 scale_fill_gradient 上工作。 (我有一个解决方法将变量转换为因子并使用 scale_fill_manual 并手动设置它们,因此我知道我的库和一般 ggplot 结构是正确的。这是我的第一个示例。)我已经根据帮助文本完成了所有工作,但是在第二个示例中,它似乎根本没有识别出我的填充。

 > str(data)

'data.frame':   506 obs. of  9 variables:
 $ value_A              : int  250000 219000 358000 340000 364000 295000 
 $ factor_B             : Factor w/ 6 levels "4","5","6","7",..: 4 3 4 4 4 3 
 $ numeric_C             : num  15.3 17.8 17.8 18.7 18

这很好用(它在一个因子变量上):

ggplot(data, aes(value, fill = factor_B)) +
  geom_histogram(binwidth = 30000) +  ...
  scale_fill_manual(values = ("#9db5bf", "#85a3b0","#6c90a0","#547e90", "#3b6b80","#235970"))

这不起作用,它用灰色填充条形,我也没有得到任何类型的图例:(这是在数字变量上)

    ggplot(data, aes(value, fill = numeric_C)) +
  geom_histogram(binwidth = 30000)  + ...
  scale_fill_gradient("some Ratio", low = "#9db5bf", high = "#235970")

【问题讨论】:

  • 我想我也有同样的问题,你有没有解决过这个问题?

标签: r


【解决方案1】:

试试这个:

ggplot(data, aes(value, fill = numeric_C)) +
  geom_histogram(aes(fill = ..count..), binwidth = 30000)  + # ...
  scale_fill_gradient("some Ratio", low = "#9db5bf", high = "#235970")

【讨论】:

    猜你喜欢
    • 2019-12-24
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    相关资源
    最近更新 更多