gpt4 book ai didi

r - geom_bar 基于值使用 geom_point 颜色对数据进行分组

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

我想将显示 sect 值的 geom_point 添加到我的分组 geom_bar 中,显示基于条件数据的点颜色的 perc 值
我的数据是这样的:

```structure(list(year = c(2019, 2019, 2019, 2018, 2018, 2018, 2017, 
2017, 2017, 2020, 2020, 2020), ques = c("q1", "q2", "q3", "q1",
"q2", "q3", "q1", "q2", "q3", "q1", "q2", "q3"), sect = c(94.25,
99, 86.33, 94.19, 92.21, 90.42, 98.88, 89.31, 82.76, 82.06, 75.46,
59.39), perc = c(88.79, 94.71, 89.46, 89.69, 81.29, 84.1, 90.18,
89.1, 73.38, 70.66, 97, 75.99), diff = c(-5.46, -4.29, 3.13,
-4.5, -10.92, -6.32, -8.7, -0.21, -9.38, -11.4, 21.54, 16.6),
ci = c(12.76572759, 12.76572759, 12.76572759, 12.76572759,
12.76572759, 12.76572759, 12.76572759, 12.76572759, 12.76572759,
12.76572759, 12.76572759, 12.76572759)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L), spec = structure(list(
cols = list(year = structure(list(), class = c("collector_double",
"collector")), ques = structure(list(), class = c("collector_character",
"collector")), sect = structure(list(), class = c("collector_double",
"collector")), perc = structure(list(), class = c("collector_double",
"collector")), diff = structure(list(), class = c("collector_double",
"collector")), ci = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
表是这样的:
```    year ques   sect  perc   diff    ci
<dbl> <chr> <dbl> <dbl> <dbl> <dbl>
1 2019 q1 94.2 88.8 -5.46 12.8
2 2019 q2 99 94.7 -4.29 12.8
3 2019 q3 86.3 89.5 3.13 12.8
4 2018 q1 94.2 89.7 -4.5 12.8
5 2018 q2 92.2 81.3 -10.9 12.8
6 2018 q3 90.4 84.1 -6.32 12.8
7 2017 q1 98.9 90.2 -8.7 12.8
8 2017 q2 89.3 89.1 -0.21 12.8
9 2017 q3 82.8 73.4 -9.38 12.8
10 2020 q1 82.1 70.7 -11.4 12.8
11 2020 q2 75.5 97 21.5 12.8
12 2020 q3 59.4 76.0 16.6 12.8
简单的代码:
```df %>% 
ggplot(aes(fill=ques, y = perc, x = year)) +
geom_bar(position="dodge", stat="identity")+
geom_point()
我想要的颜色条件是:
```df <- df %>%
mutate(
color = case_when(
diff >= ci ~ "green",
diff <= -ci ~ "red",
TRUE ~ "grey"
)
)
作为第一步,我无法让点出现在相关条上: this shows points vertically but I would like them horizontal over relevant years因此,我将非常感谢您帮助显示相关年份的点数,并结合颜色条件以在 diff >= ci 时显示绿色,红色 <= - ci 和灰色均不显示。希望这是有道理的,谢谢

最佳答案

您可以使用 position_dodgegeom_point调用以将点与条形匹配。要获得正确的颜色,您可以映射新列 color到色标并添加对 scale_color_identity 的调用.
如果我正确理解你的逻辑,你的数据框中的任何点都不会变成红色,因为 diff 的值都没有。小于 -12.8

df %>%   
mutate(color = case_when(diff >= ci ~ "green",
diff <= -ci ~ "red",
TRUE ~ "gray20")) %>%
ggplot(aes(fill=ques, y = perc, x = year)) +
geom_bar(position="dodge", stat="identity")+
geom_point(aes(color = color),
position = position_dodge(width = 0.9), size = 3) +
scale_color_identity()
enter image description here

关于r - geom_bar 基于值使用 geom_point 颜色对数据进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64837113/

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