【问题标题】:Inaccurate mapping of gradient fill colours with bin counts in `geom_hexbin` in `ggplot2`渐变填充颜色与 ggplot2 中的 geom_hexbin 中的 bin 计数映射不准确
【发布时间】:2023-02-11 20:55:39
【问题描述】:

我正在尝试使用 ggplot2 绘制如下所示的分箱散点图。

library(ggplot2)
bks = seq(from = 0, to = 10000, by = 1000)
d <- ggplot(diamonds, aes(carat, price)) + theme_bw()
d + geom_point(alpha = 0.01)

当我使用 geom_hexbin 时,bin 中的计数未准确映射到梯度比例。

d + geom_hex(aes(fill = after_stat(count)), bins = 30, colour = "white") + 
  scale_fill_distiller(palette = "Spectral", breaks = bks) +
  geom_text(data = diamonds, aes(x = carat, y = price, label = after_stat(count)),
          stat="binhex", bins=30, show.legend=FALSE,
          colour="black", size=2.5)

例如,计数为 5809 和 5556 的 bin 仍显示为蓝色。

然而,对于geom_bin_2d,映射似乎是准确的

d + geom_bin_2d(aes(fill = after_stat(count)), bins = 30) + 
  scale_fill_distiller(palette = "Spectral", breaks = bks) +
  geom_text(data = diamonds, aes(x = carat, y = price, label = after_stat(count)),
            stat="bin_2d", bins=30, show.legend=FALSE,
            colour="black", size=2.5)

这里出了什么问题?如何在 geom_hexbinggplot2 中使用填充渐变颜色准确映射十六进制数?

【问题讨论】:

    标签: r ggplot2 visualization binning


    【解决方案1】:

    更新

    CRAN 上的最新版本 ggplot2 3.4.1 应该已修复此错误。

    旧答案

    这是 ggplot2 开发版本中修复的已知错误。您可以使用下面的命令来安装最新的 ggplot2 开发版本:

    devtools::install_github("tidyverse/ggplot2")
    

    相关讨论见https://github.com/tidyverse/ggplot2/issues/504

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-23
      • 1970-01-01
      • 2018-08-30
      • 2014-01-31
      • 2012-07-31
      • 1970-01-01
      • 2016-09-09
      • 1970-01-01
      相关资源
      最近更新 更多