【问题标题】:How to plot points using hexagonal binning with user defined colors in ggplot2如何在 ggplot2 中使用用户定义颜色的六边形分箱来绘制点
【发布时间】:2017-04-06 03:47:16
【问题描述】:

我想使用六边形分箱绘制 X 和 Y 的“散点图”。目前,我有这个代码:

library(ggplot2)
X=rnorm(1000)
Y=rnorm(1000)
Z=sin(X)
df = data.frame(X,Y,Z)

p = ggplot(df, aes(X, Y)) + 
  stat_binhex()
plot(p)

代码生成一个图,其中每个六边形的颜色代表 bin 中的点数。我希望颜色反映我的 df 中的 Z 变量(因为点是分箱的,所以颜色反映分箱 Z 的平均值)。我该怎么做?

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    我认为你需要stat_summary_hex 并且可以使用scale_fill_continuous 指定颜色:

    ggplot(df, aes(X, Y)) +
        stat_summary_hex(aes(z = Z)) +
        scale_fill_continuous(low = 'blue', high = 'red')
    

    【讨论】:

    • 谢谢!这行得通。我唯一不同的是使用 scale_fill_distiller(palette = "RdBu") 使色阶看起来更好
    猜你喜欢
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多