gpt4 book ai didi

r - 如何使用条件语句为条形图着色

转载 作者:行者123 更新时间:2023-12-05 09:36:49 26 4
gpt4 key购买 nike

<分区>

我正在尝试根据样本 ifelse(total > 90, "#FC2D00", "#008EFC") 的数量为 geom_bar 着色。换句话说,如果 total > 90,则条形应该是 红色total < 90,它应该是蓝色。

type = c("aa", "bb", "cc")
total = c(110, 90, 89)

df = data.frame(type, total)

df %>%
ggplot2::ggplot(aes(x = type, y = total)) +
geom_bar(position = "dodge",
stat = "identity")

我试过了

df %>% 
ggplot2::ggplot(aes(x = type, y = total)) +
geom_bar(position = "dodge",
stat = "identity",
fill = (ifelse(
levels(studies$total > 90, "#FC2D00", "#008EFC"))))

还有

df %>% 
mutate(fill = ifelse(levels(total > 90, "#FC2D00", "#008EFC"))) %>%
ggplot2::ggplot(aes(x = type, y = total)) +
geom_bar(position = "dodge",
stat = "identity",
fill = fill)

但还是不行。我不确定是什么问题。

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