You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ggplot2中移除柱状图内的百分比与水平线

问题修复与图表优化方案

核心问题分析

你的代码中错误使用了gather()函数,将Frequency_percent和percent两列合并成同一列,导致每个分类(Categories)对应两行重复数据,最终生成的柱状图里出现的"内部水平线"实际是两个重叠的细柱子;同时重复数据也会让百分比标签重复显示。

修复步骤

1. 移除错误的宽转长操作

直接使用原数据中的percent列即可,无需执行gather()——这是导致内部线的根本原因。

2. 简化数据处理流程

跳过冗余的data_long和merge步骤,直接对原数据过滤并映射短标签:

# 过滤percent>0的有效数据
data_filtered <- data[data$percent > 0, ]

# 直接添加短标签,无需merge操作
data_filtered$Short_Labels <- factor(data_filtered$Variables, 
                                     levels = unique(data$Variables), 
                                     labels = LETTERS[1:length(unique(data$Variables))])

3. 确保仅顶部显示百分比

原geom_text逻辑正确,数据修正后会自动只显示每个柱子顶部的百分比,无需额外调整标签逻辑。

图表优化建议

  • 调整柱子与文本位置:缩小position_dodge宽度,让柱子和标签对齐更紧凑,避免重叠
  • 优化图例布局:将图例分成多列,避免底部图例过长
  • 扩展y轴范围:防止顶部百分比标签超出图表边界
  • 统一视觉风格:调整标题、轴文本样式,使用多列图例提升可读性

完整修正代码

data <- data.frame(
  Variables = c(rep("A", 5), rep("B", 5), rep("C", 5), rep("D", 5), rep("E", 2),
                rep("F", 5), rep("G", 5), rep("H", 5), rep("I", 5), rep("J", 5)),
  Categories = c("Always", "Often", "Occasionally", "Rarely", "Never",
                 "Always", "Often", "Occasionally", "Rarely", "Never",
                 "Very effective", "Effective", "Neutral", "Ineffective", "Very ineffective",
                 "Always", "Often", "Occasionally", "Rarely", "Never",
                 "Yes", "No",
                 "Strongly agree", "Agree", "Neutral", "Disagree", "Strongly disagree",
                 "Very effective", "Effective", "Neutral", "Ineffective", "Very ineffective",
                 "Strongly agree", "Agree", "Neutral", "Disagree", "Strongly disagree",
                 "Very engaged", "Engaged", "Neutral", "Disengaged", "Very disengaged",
                 "Very likely", "Likely", "Neutral", "Unlikely", "Very unlikely"),
  Frequency_percent = c(8, 3, 1, 0, 0,
                        0, 9, 2, 1, 0,
                        0, 10, 2, 0, 0,
                        0, 10, 1, 0, 1,
                        11, 1,
                        9, 2, 1, 0, 0,
                        10, 1, 1, 0, 0,
                        7, 4, 1, 0, 0,
                        6, 5, 1, 0, 0,
                        4, 5, 3, 0, 0),
  percent = c(67, 25, 8, 0, 0,
              0, 75, 16,8, 0,
              0, 83, 17, 0, 0,
              0, 84, 8, 0, 8,
              92, 8,
              75, 17, 8, 0, 0,
              84, 8, 8, 0, 0,
              59, 33, 8, 0, 0,
              50, 42, 8, 0, 0,
              33, 42, 25, 0, 0)
  
)

# 过滤有效数据并添加短标签
data_filtered <- data[data$percent > 0, ]
data_filtered$Short_Labels <- factor(data_filtered$Variables, 
                                     levels = unique(data$Variables), 
                                     labels = LETTERS[1:length(unique(data$Variables))])

# 绘制优化后的图表
ggplot(data_filtered, aes(x = Short_Labels, y = percent, fill = Categories)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.8), color = "black", width = 0.7) +
  labs(title = "学生反馈统计",
       y = "占比 (%)",
       fill = "分类") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 0, hjust = 0.5, size = 10),
        axis.title.x = element_blank(),
        axis.text.y = element_text(color = "black"),
        legend.position = "bottom",
        plot.title = element_text(hjust = 0.5, size = 14, face = "bold")) +
  guides(fill = guide_legend(title = "反馈分类", ncol = 3)) +  # 图例分3列展示
  ylim(0, 100) +  # 扩展y轴范围,避免标签溢出
  geom_text(position = position_dodge(width = 0.8), vjust = -0.3, size = 3, 
            color = "black", aes(label = paste0(percent, "%")))

内容的提问来源于stack exchange,提问作者niko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 16:15:41