【发布时间】: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_hexbin 和 ggplot2 中使用填充渐变颜色准确映射十六进制数?
【问题讨论】:
标签: r ggplot2 visualization binning