如何使用条件语句为条形图着色
How to use conditional statement to colour barplot
我正在尝试根据样本数 ifelse(total > 90, "#FC2D00", "#008EFC")
为 geom_bar
上色。换句话说,如果 total
> 90,条形应该是 red
但 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)
但它仍然无法正常工作。我不确定是什么问题。
将您的填充移动到 aes()
内。
您应该在 scale_fill_manual()
中指定颜色:
df %>%
mutate(fill = ifelse(levels(total > 90, "#FC2D00", "#008EFC"))) %>%
ggplot2::ggplot(aes(x = type, y = total, fill = fill)) +
geom_bar(position = "dodge",
stat = "identity") +
scale_fill_manual(values= c("red","blue"))
在 ggplot 中,我们通常在比例尺中定义颜色值,并且图例会根据数据很好地自动生成。因此,将数据放入您想要的图例标签,而不是您想要使用的颜色。
df %>%
mutate(emphasize = ifelse(total > 90, "> 90", "<= 90")) %>%
ggplot(aes(x = type, y = total, fill = emphasize)) +
geom_col(position = "dodge") +
scale_fill_manual(values = c("> 90" = "#FC2D00", "<= 90" = "#008EFC"))
我们不需要使用 levels()
- 这是一个糟糕的选择,因为 total
不是一个因素(而且它不是每行 return 一个值,它可能与数据的顺序不同......)。我还将 geom_bar
切换为 geom_col
- geom_col
默认为 stat = "identity"
。
不需要ifelse()
或mutate()
。您可以直接使用 fill
中的逻辑条件,然后使用 scale_fill_manual()
格式化颜色和标签:
library(ggplot2)
library(dplyr)
#Data
type = c("aa", "bb", "cc")
total = c(110, 90, 89)
df = data.frame(type, total)
#Plot
df %>%
ggplot2::ggplot(aes(x = type, y = total,fill=total > 90)) +
geom_bar(position = "dodge",
stat = "identity")+
scale_fill_manual(values = c("#FC2D00","#008EFC"),
labels=c('TRUE'='>90','FALSE'='<90'))+
labs(fill='Total')
输出:
我正在尝试根据样本数 ifelse(total > 90, "#FC2D00", "#008EFC")
为 geom_bar
上色。换句话说,如果 total
> 90,条形应该是 red
但 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)
但它仍然无法正常工作。我不确定是什么问题。
将您的填充移动到 aes()
内。
您应该在 scale_fill_manual()
中指定颜色:
df %>%
mutate(fill = ifelse(levels(total > 90, "#FC2D00", "#008EFC"))) %>%
ggplot2::ggplot(aes(x = type, y = total, fill = fill)) +
geom_bar(position = "dodge",
stat = "identity") +
scale_fill_manual(values= c("red","blue"))
在 ggplot 中,我们通常在比例尺中定义颜色值,并且图例会根据数据很好地自动生成。因此,将数据放入您想要的图例标签,而不是您想要使用的颜色。
df %>%
mutate(emphasize = ifelse(total > 90, "> 90", "<= 90")) %>%
ggplot(aes(x = type, y = total, fill = emphasize)) +
geom_col(position = "dodge") +
scale_fill_manual(values = c("> 90" = "#FC2D00", "<= 90" = "#008EFC"))
我们不需要使用 levels()
- 这是一个糟糕的选择,因为 total
不是一个因素(而且它不是每行 return 一个值,它可能与数据的顺序不同......)。我还将 geom_bar
切换为 geom_col
- geom_col
默认为 stat = "identity"
。
不需要ifelse()
或mutate()
。您可以直接使用 fill
中的逻辑条件,然后使用 scale_fill_manual()
格式化颜色和标签:
library(ggplot2)
library(dplyr)
#Data
type = c("aa", "bb", "cc")
total = c(110, 90, 89)
df = data.frame(type, total)
#Plot
df %>%
ggplot2::ggplot(aes(x = type, y = total,fill=total > 90)) +
geom_bar(position = "dodge",
stat = "identity")+
scale_fill_manual(values = c("#FC2D00","#008EFC"),
labels=c('TRUE'='>90','FALSE'='<90'))+
labs(fill='Total')
输出: