如何移除R语言柱状图中数值为0的柱子的轮廓线?
移除ggplot分组柱状图中0值柱子的多余轮廓线
你的分组柱状图里,数值为0的柱子仍显示黑色轮廓线,要解决这个问题,只需让柱子的轮廓色根据final_value是否为0动态调整:当值为0时,将轮廓色设为与背景一致(theme_classic()的背景是白色),非0值则保留黑色轮廓。
修改后的完整代码
library(tidyverse) #generate data Correlation_task_persistence <- c("0.29", "0.29","0.24", "0.20", "0.24", "0.04", "0.00", "0.04") Biv_A_task_persistence <- c("80%", "80%","44%", "44%", "47%", "72%", "49%", "55%") Biv_E_task_persistence <- c("20%", "20%", "57%", "56%", "54%", "29%", "52%", "45%") Correlation_activity <- c("0.29", "0.29", NA, "0.29", "0.08", "0.08", "0.07", "0.00") Biv_A_activity <- c("80%", "80%", NA, "80%", "45%", "70%", "20%", "50%") Biv_E_activity <- c("20%", "20%", NA, "20%", "55%", "40%", "90%", "50%") age <- c("intercept", "slope", "36", "30", "24", "18", "12", "6") Correlation_emotionality <- c("0.19", "0.19","0.34", "0.10", "0.13", "0.04", "0.00", "0.04") Biv_A_emotionality <- c("80%", "80%","43%", "44%", "47%", "71%", "49%", "55%") Biv_E_emotionality <- c("20%", "20%", "57%", "56%", "53%", "29%", "51%", "45%") df.new <- data.frame(Correlation_task_persistence, Biv_A_task_persistence,Biv_E_task_persistence, Correlation_activity, Biv_A_activity, Biv_E_activity, Correlation_emotionality, Biv_A_emotionality, Biv_E_emotionality, age) #produce the bar plot df.new %>% mutate(across(Correlation_task_persistence:Biv_E_emotionality, ~if_else(as.numeric(gsub("%", "", .x)) > 1, as.numeric(gsub("%", "", .x, fixed = TRUE))/100, as.numeric(.x)))) %>% pivot_longer(-c(age, contains("Correlation"))) %>% mutate(Correlation = case_when( grepl("task", name) ~ Correlation_task_persistence, grepl("activity", name) ~ Correlation_activity, grepl("emotionality", name) ~ Correlation_emotionality), final_value = Correlation * value, name = gsub("_task", "", name)) %>% tidyr::extract("name", c("var","group"), regex = "(.*)_([^_]+)$") %>% group_by(age, group) %>% mutate(label = scales::percent(final_value / sum(final_value))) %>% ungroup() %>% mutate(age = factor(age, levels = c("0", "6", "12", "18", "24", "30", "36", "intercept", "slope"), ordered = TRUE)) %>% ggplot(aes(x = age, y = final_value, fill = var)) + # 核心修改:根据final_value是否为0设置轮廓色 geom_col(aes(color = final_value != 0)) + scale_color_manual(values = c("FALSE" = "white", "TRUE" = "black"), guide = "none") + theme_classic() + geom_text(aes(label = Correlation, group = age), stat = 'summary', fun = function(x) sum(x) + 0.01 * sign(x), size = 3) + geom_text(aes(label = label), size = 3, position = position_stack(vjust = 0.5)) + scale_fill_grey(start = 0.475, end = 0.8, na.value = "red") + labs(y = "Correlation") + labs(x = "") + theme(legend.position = "bottom", legend.title = element_blank()) + facet_wrap(~group)
关键修改点说明
- 修正原代码逻辑错误:将
if_else多分支判断改为case_when,避免语法报错 - 动态设置轮廓色:把
geom_col(color="black")改为geom_col(aes(color = final_value != 0)),让轮廓色绑定到final_value是否非0的逻辑值 - 自定义颜色映射:添加
scale_color_manual(),将0值对应的轮廓色设为白色(与背景一致),非0值设为黑色,同时隐藏额外图例
这样处理后,0值的柱子轮廓会和背景完全融合,不会再显示多余的线条。
内容的提问来源于stack exchange,提问作者wooden05
相关产品推荐
相关产品推荐

