gpt4 book ai didi

r - geom_hex 的对数比例密度着色

转载 作者:行者123 更新时间:2023-12-04 00:30:23 24 4
gpt4 key购买 nike

geom_hex几何在 ggplot2根据落入其中的点数为六边形箱着色。这对于均匀分布的数据非常有效,但如果某些区域比其他区域更密集,则效果不佳 - 差异可能会被单个非常密集的六边形的存在所淹没。

如何使密度色标使用对数标度或其他类型的归一化转换?

最佳答案

ggplot 3.0+ 通过新的 stat() 揭开了汇总指标计算的神秘面纱内部功能。这样可以更轻松地修改用于创建六边形填充的统计数据。例如:

默认计数统计

df <- data.frame(
x = rnorm(1000),
y = rnorm(1000)
)

plot.df <- ggplot(data = df, aes(x = x, y = y)) +
geom_hex(aes(fill = stat(count)))
print(plot.df)

enter image description here

日志计数统计
plot.df.log <- ggplot(data = df, aes(x = x, y = y)) +
geom_hex(aes(fill = stat(log(count))))
print(plot.df.log)

enter image description here

代替 log ,你可以做任何你想做的任意变换,比如立方根等。

使用 cut
为避免创建具有混淆值的比例,您可以使用 cut建立合理的类别边界,并将它们转换为标有原始计数值的数字刻度:
plot.df.log.cut <- ggplot(data = df, aes(x = x, y = y)) +
geom_hex(aes(fill = stat(cut(log(count), breaks = log(c(0, 1, 2, 4, Inf)), labels = F, right = T, include.lowest = T)))) +
scale_fill_continuous(name = 'count', labels = c('1', '2', '4', '8+'))
print(plot.df.log.cut)

enter image description here

关于r - geom_hex 的对数比例密度着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52448815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com