如何将ggplot堆叠条形图修改为对应三选项的普通条形图
修改堆叠条形图为普通条形图的实现方案
我现有代码可生成符合预期的堆叠条形图,但需要制作一个包含“yes”“no”“maybe”三个独立条形的普通条形图,不清楚如何修改现有代码,特此请教可行方案。
当前代码
mydata %>% mutate( variable = recode( variable, 'item3' = 'item name here')) %>% drop_na() %>% ggplot(mapping = aes(x = variable)) + geom_bar(aes(fill = value), position = 'fill', width = 0.30)+ scale_y_continuous(name = 'Percentage', breaks = seq(from = 0, to = 1, by = 0.2), minor_breaks = seq(from = 0, to = 1, by = 0.1), labels = seq(from = 0, to = 100, by = 20), expand = c(0,0)) + xlab( element_blank() ) + scale_fill_manual( values = c(colour="red"), #if this was stacked bar chart, here would be a color palette labels = c( 'yes', 'no', 'maybe' ), drop = FALSE ) + guides( fill = guide_legend(title = 'Answer') ) + theme(panel.background = element_blank(), axis.ticks.y = element_blank(), panel.grid.major = element_line(colour = 'grey'), axis.ticks.x = element_line(colour = 'grey'), panel.grid.minor = element_line(colour = 'lightgrey'), axis.text.y = element_text( size = 11, face = 'bold', hjust = 0 )) + coord_flip()
所用数据
structure(list(variable = c("item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3"), value = structure(c(1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("1", "2", "3"), class = c("ordered", "factor"))), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -147L))
修改后的代码(普通条形图)
mydata %>% drop_na() %>% # 把value的因子标签替换成yes/no/maybe,直接映射到x轴 mutate(value = recode(value, '1' = 'yes', '2' = 'no', '3' = 'maybe')) %>% ggplot(aes(x = value, fill = value)) + # 普通条形图,移除堆叠参数position='fill' geom_bar(width = 0.30) + # 若需显示百分比而非计数,替换为以下两行代码 # geom_bar(aes(y = after_stat(prop), group = 1), width = 0.30) + # scale_y_continuous(name = 'Percentage', # breaks = seq(0, 1, 0.2), # labels = paste0(seq(0, 100, 20), '%'), # expand = c(0,0)) + scale_y_continuous(name = 'Count', expand = c(0,0)) + xlab('Answer') + # 为三个选项配置不同填充色 scale_fill_manual( values = c('yes' = '#E63946', 'no' = '#457B9D', 'maybe' = '#F1FAEE'), drop = FALSE ) + guides(fill = guide_legend(title = 'Answer')) + theme(panel.background = element_blank(), axis.ticks.y = element_blank(), panel.grid.major = element_line(colour = 'grey'), axis.ticks.x = element_line(colour = 'grey'), panel.grid.minor = element_line(colour = 'lightgrey'), axis.text.y = element_text(size = 11, face = 'bold', hjust = 0)) # 不需要翻转坐标轴可删除此行 # coord_flip()
关键修改说明
- 调整x轴映射:将原代码中
x = variable改为x = value,并提前把value的因子标签替换为“yes”“no”“maybe”,让每个答案选项对应一个独立条形。 - 移除堆叠参数:删除
geom_bar中的position = 'fill',该参数用于生成堆叠百分比图,普通条形图无需保留。 - 灵活设置y轴:默认显示计数;若需展示百分比,使用
geom_bar(aes(y = after_stat(prop), group = 1))计算各选项占比,并对应调整y轴标签格式。 - 完善颜色配置:为三个选项分别设置不同填充色,替换原代码中单一的颜色值。
- 简化变量处理:因无需展示
variable信息,去掉了原代码中对variable的重编码步骤,若需保留可自行添加。
内容的提问来源于stack exchange,提问作者Espejito
相关产品推荐
相关产品推荐

