如何根据 ggplot2 中的某些条件删除标签?
How do I remove labels based on certain conditions in ggplot2?
我正在参加#duboischallenge 并且正在进行挑战 5。
我想删除绘图上的标签“0%”。
代码(仍在学习中,所以可能不是最有效的代码!):
income <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-02-16/income.csv')
data5 <- income %>%
gather(expenses, percentage, 3:7) %>%
mutate(percentage = replace_na(percentage, 0))
data5$expenses <- factor(data5$expenses, levels = c("Other", "Tax", "Clothes", "Food", "Rent"))
data5$Class <-factor(data5$Class, levels = c("Over 00", "0-1000", "0-750", "0-500", "0-400", "0-300", "0-200"))
plot5 <-ggplot(data5, aes(x = Class, y = percentage, fill = expenses)) +
geom_bar(position = "fill", stat = "identity")+
scale_fill_manual(values = c("black", "blueviolet", "darksalmon", "azure3", "cornsilk3"), breaks = c("Rent", "Food","Clothes", "Tax", "Other"))+
geom_text(aes(x = Class, y = percentage, label = paste0(percentage, "%")), color = if_else(data5$expenses == "Rent", "white", "black"), size = 3.5, position = position_fill(vjust = 0.5)) +
coord_flip()
如果我添加
geom_text(data = data5 %>% filter(percentage > 3),aes(x = Class, y = percentage, label = paste0(percentage, "%")), color = if_else(data5$expenses == "Rent", "white", "black"), size = 3.5, position = position_fill(vjust = 0.5))
我收到这个错误:
错误:美学必须是长度1或与数据相同(32):颜色
如何删除“0%”标签?
您可以简单地采用与为标签着色相同的方法:
ggplot(data5, aes(x = Class, y = percentage, fill = expenses)) +
geom_bar(position = "fill", stat = "identity")+
scale_fill_manual(values = c("black", "blueviolet", "darksalmon", "azure3", "cornsilk3"), breaks = c("Rent", "Food","Clothes", "Tax", "Other"))+
geom_text(aes(x = Class, y = percentage,
label = ifelse(percentage > 0, paste0(percentage, "%"), "")),
color = if_else(data5$expenses == "Rent", "white", "black"), size = 3.5, position = position_fill(vjust = 0.5)) +
coord_flip()
我正在参加#duboischallenge 并且正在进行挑战 5。
我想删除绘图上的标签“0%”。
代码(仍在学习中,所以可能不是最有效的代码!):
income <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-02-16/income.csv')
data5 <- income %>%
gather(expenses, percentage, 3:7) %>%
mutate(percentage = replace_na(percentage, 0))
data5$expenses <- factor(data5$expenses, levels = c("Other", "Tax", "Clothes", "Food", "Rent"))
data5$Class <-factor(data5$Class, levels = c("Over 00", "0-1000", "0-750", "0-500", "0-400", "0-300", "0-200"))
plot5 <-ggplot(data5, aes(x = Class, y = percentage, fill = expenses)) +
geom_bar(position = "fill", stat = "identity")+
scale_fill_manual(values = c("black", "blueviolet", "darksalmon", "azure3", "cornsilk3"), breaks = c("Rent", "Food","Clothes", "Tax", "Other"))+
geom_text(aes(x = Class, y = percentage, label = paste0(percentage, "%")), color = if_else(data5$expenses == "Rent", "white", "black"), size = 3.5, position = position_fill(vjust = 0.5)) +
coord_flip()
geom_text(data = data5 %>% filter(percentage > 3),aes(x = Class, y = percentage, label = paste0(percentage, "%")), color = if_else(data5$expenses == "Rent", "white", "black"), size = 3.5, position = position_fill(vjust = 0.5))
我收到这个错误: 错误:美学必须是长度1或与数据相同(32):颜色
如何删除“0%”标签?
您可以简单地采用与为标签着色相同的方法:
ggplot(data5, aes(x = Class, y = percentage, fill = expenses)) +
geom_bar(position = "fill", stat = "identity")+
scale_fill_manual(values = c("black", "blueviolet", "darksalmon", "azure3", "cornsilk3"), breaks = c("Rent", "Food","Clothes", "Tax", "Other"))+
geom_text(aes(x = Class, y = percentage,
label = ifelse(percentage > 0, paste0(percentage, "%"), "")),
color = if_else(data5$expenses == "Rent", "white", "black"), size = 3.5, position = position_fill(vjust = 0.5)) +
coord_flip()