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

如何调整R-ggplot2条形图的颜色与标签顺序为w1、w2、w3?

问题:调整ggplot2条形图的图例与条形顺序一致

我尝试调整条形图的标签顺序,希望标签顺序与条形顺序一致,即按w1、w2、w3排列,但未达到预期效果。以下是我的代码:

rm(list = ls())
library(ggplot2)
library(tidyr)
library(tidyverse)

df <- data.frame(
  experience = c("C Exp", "C Exp", "No E Exp", "No E Exp", "Other E Exp", "Other E Exp"),
  overall_rec = c("Agree", "Disagree", "Agree", "Disagree", "Agree", "Disagree"),
  pct_overall_w1 = c(0.6326530612, 0.3673469388, 0.8604651163, 0.1395348837, 0.7465277778, 0.2534722222),
  pct_overall_w2 = c(0.05882352941, 0.9411764706, 0.06097560976, 0.9390243902, 0.05211726384, 0.9478827362),
  pct_overall_w3 = c(0.04615384615, 0.9538461538, 0.1, 0.9, 0.08076009501, 0.919239905)
)

# Filter the data frame to include only rows where overall_rec = Agree
df_filtered <- subset(df, overall_rec == "Agree") %>% 
  gather(key = "pct_overall", value = "percentage", pct_overall_w1:pct_overall_w3) %>% 
  ungroup() %>% 
  select(experience, pct_overall, percentage) %>% 
  arrange(experience)

df_filtered$pct_overall <- factor(df_filtered$pct_overall, levels = c("pct_overall_w3", "pct_overall_w2", "pct_overall_w1"))


# Create the dodged bar chart with updated legend labels and percentage labels
ggplot(df_filtered, aes(x = experience, y = percentage, fill = pct_overall)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Percentage of Overall Recommendation by Experience",
       x = "Experience",
       y = "Percentage",
       fill = "Recommendation") +
  scale_fill_manual(values = c("#1f77b4", "#2ca02c", "#d62728"),
                    labels = c("w3", "w2", "w1")) +
  geom_text(aes(label = paste0(round(percentage * 100), "%")), 
            position = position_dodge(width = 0.9), 
            vjust = -0.5, 
            size = 3) +
  theme_classic() +
  coord_flip()

当前图表中,每个experience分组下的条形从左到右依次是w1(红色)、w2(绿色)、w3(蓝色),但图例显示的顺序是w3、w2、w1,两者不匹配。


解决方法

问题出在两处设置的顺序完全反了:

  1. 因子层级顺序错误:你把pct_overall的因子层级设成了c("pct_overall_w3", "pct_overall_w2", "pct_overall_w1"),这会让ggplot按w3→w2→w1的逻辑排序,但你想要的是w1→w2→w3。
  2. 图例标签与颜色映射顺序错误:scale_fill_manual里的标签和颜色顺序也和目标顺序相反,导致图例和条形对应不上。

修改后的完整代码

rm(list = ls())
library(ggplot2)
library(tidyr)
library(tidyverse)

df <- data.frame(
  experience = c("C Exp", "C Exp", "No E Exp", "No E Exp", "Other E Exp", "Other E Exp"),
  overall_rec = c("Agree", "Disagree", "Agree", "Disagree", "Agree", "Disagree"),
  pct_overall_w1 = c(0.6326530612, 0.3673469388, 0.8604651163, 0.1395348837, 0.7465277778, 0.2534722222),
  pct_overall_w2 = c(0.05882352941, 0.9411764706, 0.06097560976, 0.9390243902, 0.05211726384, 0.9478827362),
  pct_overall_w3 = c(0.04615384615, 0.9538461538, 0.1, 0.9, 0.08076009501, 0.919239905)
)

# Filter the data frame to include only rows where overall_rec = Agree
df_filtered <- subset(df, overall_rec == "Agree") %>% 
  gather(key = "pct_overall", value = "percentage", pct_overall_w1:pct_overall_w3) %>% 
  ungroup() %>% 
  select(experience, pct_overall, percentage) %>% 
  arrange(experience)

# 调整因子层级为w1→w2→w3的顺序
df_filtered$pct_overall <- factor(df_filtered$pct_overall, levels = c("pct_overall_w1", "pct_overall_w2", "pct_overall_w3"))


# Create the dodged bar chart with updated legend labels and percentage labels
ggplot(df_filtered, aes(x = experience, y = percentage, fill = pct_overall)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Percentage of Overall Recommendation by Experience",
       x = "Experience",
       y = "Percentage",
       fill = "Recommendation") +
  # 同步调整颜色和标签顺序,与条形顺序对应
  scale_fill_manual(values = c("#d62728", "#2ca02c", "#1f77b4"),
                    labels = c("w1", "w2", "w3")) +
  geom_text(aes(label = paste0(round(percentage * 100), "%")), 
            position = position_dodge(width = 0.9), 
            vjust = -0.5, 
            size = 3) +
  theme_classic() +
  coord_flip()

核心修改点

  • 将pct_overall的因子层级改为c("pct_overall_w1", "pct_overall_w2", "pct_overall_w3"),确保条形按w1→w2→w3的顺序绘制
  • 调整scale_fill_manual中的values和labels顺序,让颜色、标签和条形顺序一一对应,这样图例顺序就和条形顺序完全匹配了

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 07:24:59