gpt4 book ai didi

r - 按 ggplot2 geom_count 中的出现次数对点进行着色

转载 作者:行者123 更新时间:2023-12-01 21:40:49 24 4
gpt4 key购买 nike

我想根据 ggplot2 的 geom_count 绘制的点的计数来为其着色。

这是我到目前为止所拥有的:

ggplot(test3, aes(eleStart, eleLength)) +  geom_count(aes(alpha=0.25, color= ..prop..)) +
scale_y_continuous(breaks=seq(0,130,5)) +
scale_x_continuous(breaks=seq(0,114)) +
theme(panel.grid.minor = element_blank())

现在我基本上只想将 color=..prop..geom_count 计算出的实际计数交换,而不是它们的比例。

test3 数据框如下所示:

# A tibble: 294 x 2
# Groups: X1 [56]
eleStart eleLength
<int> <int>
1 0 3
2 0 6
3 0 7
4 0 9
5 0 11
6 0 23
7 0 25
8 0 26
9 0 26
10 0 26
# ... with 284 more rows

最佳答案

您可以通过 aes 中的计数转换根据点的出现情况对点进行着色。请参阅以下示例:

ggplot(mtcars, aes(cyl, carb)) + geom_count(aes(color = after_stat(n)))

enter image description here

<小时/>

如果您想“将 2 个图例合并为一个带有彩色点的图例”,请尝试以下操作:

ggplot(mtcars, aes(cyl, carb)) +
geom_count(aes(color = after_stat(n), size = after_stat(n))) +
guides(color = 'legend')

enter image description here

颜色默认显示为颜色条。在这里,guides(color = 'legend') 告诉 ggplot 将其显示为 legend,而不是单独的 colorbar .

<小时/>

要了解更多有关如何控制审美评价的信息,请参阅:https://ggplot2.tidyverse.org/reference/aes_eval.html

编辑于 2023 年 3 月 21 日

关于r - 按 ggplot2 geom_count 中的出现次数对点进行着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45883097/

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