gpt4 book ai didi

r - 在一张图中绘制计数和累积数字

转载 作者:行者123 更新时间:2023-12-02 01:43:42 25 4
gpt4 key购买 nike

我有数据如下;

dat <- structure(list(group_size = structure(c(6L, 3L, 3L, 4L, 1L, 2L, 
2L, 1L, 3L, 6L, 2L, 6L, 2L, 2L, 1L, 1L, 4L, 1L, 3L, 2L), .Label = c("(0,50]",
"(50,100]", "(100,150]", "(150,200]", "(200,250]", "(250,3e+03]"
), class = "factor"), amount = c(409, 101, 103, 198, 40, 63,
69, 49, 126, 304, 91, 401, 96, 63, 36, 1, 177, 7, 112, 61), group_sum = c(1114,
442, 442, 375, 133, 443, 443, 133, 442, 1114, 443, 1114, 443,
443, 133, 133, 375, 133, 442, 443), count = c(3L, 4L, 4L, 2L,
5L, 6L, 6L, 5L, 4L, 3L, 6L, 3L, 6L, 6L, 5L, 5L, 2L, 5L, 4L, 6L
)), row.names = c(NA, -20L), class = c("data.table", "data.frame"
))

group_size amount group_sum count
1: (250,3e+03] 409 1114 3
2: (100,150] 101 442 4
3: (100,150] 103 442 4
4: (150,200] 198 375 2
5: (0,50] 40 133 5
6: (50,100] 63 443 6
7: (50,100] 69 443 6
8: (0,50] 49 133 5
9: (100,150] 126 442 4
10: (250,3e+03] 304 1114 3
11: (50,100] 91 443 6
12: (250,3e+03] 401 1114 3
13: (50,100] 96 443 6
14: (50,100] 63 443 6
15: (0,50] 36 133 5
16: (0,50] 1 133 5
17: (150,200] 177 375 2
18: (0,50] 7 133 5
19: (100,150] 112 442 4
20: (50,100] 61 443 6

我想要绘制一个图,其中 x 轴为组大小,y 轴为 countgroup_sum

编辑:y 轴上的刻度应具有group_sum计数 可以仅在栏顶部列出一个数字。

我正在努力适应this answer by tjebo ,用于连续 x 变量:

library(tidyverse)

ggplot() +
geom_col(data = dat, aes(thevalues, n, fill = as.character(mistakes))) +
geom_density(data = dat, aes(thevalues, y = ..count..), size = 0.7, alpha = 0.1) +
scale_fill_brewer(palette = "Set1") +
theme(legend.title = element_blank(), legend.position = c(0.1, 0.85))

我必须分别用 group_sumcount 替换 thevaluesmistakes:

ggplot() +
geom_col(data = dat, aes(group_sum, n, fill = as.character(count))) +
geom_density(data = dat, aes(group_sum, y = ..count..), size = 0.7, alpha = 0.1) +
scale_fill_brewer(palette = "Set1") +
theme(legend.title = element_blank(), legend.position = c(0.1, 0.85))

期望的输出:

enter image description here

但我总是把事情搞砸......有什么建议吗?

最佳答案

类似这样的吗?

library(tidyverse)

dat <- structure(list(group_size = structure(c(
6L, 3L, 3L, 4L, 1L, 2L,
2L, 1L, 3L, 6L, 2L, 6L, 2L, 2L, 1L, 1L, 4L, 1L, 3L, 2L
), .Label = c(
"(0,50]",
"(50,100]", "(100,150]", "(150,200]", "(200,250]", "(250,3e+03]"
), class = "factor"), amount = c(
409, 101, 103, 198, 40, 63,
69, 49, 126, 304, 91, 401, 96, 63, 36, 1, 177, 7, 112, 61
), group_sum = c(
1114,
442, 442, 375, 133, 443, 443, 133, 442, 1114, 443, 1114, 443,
443, 133, 133, 375, 133, 442, 443
), count = c(
3L, 4L, 4L, 2L,
5L, 6L, 6L, 5L, 4L, 3L, 6L, 3L, 6L, 6L, 5L, 5L, 2L, 5L, 4L, 6L
)), row.names = c(NA, -20L), class = c("data.table", "data.frame"))

dat %>%
as_tibble() %>%
ggplot(aes(x = group_size)) +
geom_col(aes(y = group_sum), position = "identity", color = "red", fill = "transparent") +
geom_label(
data = dat %>% distinct(group_size, .keep_all = TRUE),
mapping = aes(y = group_sum, label = group_sum),
color = "red"
) +
geom_col(aes(y = count * 10), position = "identity", color = "blue", fill = "transparent") +
geom_label(
data = dat %>% distinct(count, .keep_all = TRUE),
mapping = aes(y = count * 10, label = count),
color = "blue"
) +
scale_y_continuous(sec.axis = sec_axis(trans = ~ . / 10, name = "Count"))

reprex package 于 2022 年 2 月 22 日创建(v2.0.0)

关于r - 在一张图中绘制计数和累积数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71217412/

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