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

在R的ggplot2中按answers分组设置折线颜色(A绿/B红)

问题描述

我有如下tibble格式的数据框:

# 数据预览
# A tibble: 24 × 4
   answers  Year Categories Satisfaction
   <chr>   <dbl> <chr>             <dbl>
 1 A        2019 Football           46.7
 2 A        2019 Basket             20.7
 3 A        2019 Volley             36.5
 4 A        2020 Football           43.7
 5 A        2020 Basket             21  
 6 A        2020 Volley             31.2
 7 A        2022 Football           49  
 8 A        2022 Basket             17.9
 9 A        2022 Volley             34.2
10 A        2023 Football           33.8

完整数据结构:

df = structure(list(answers = c("A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B"), Year = c(2019, 2019, 2019, 2020, 2020, 2020, 
2022, 2022, 2022, 2023, 2023, 2023, 2019, 2019, 2019, 2020, 2020, 
2020, 2022, 2022, 2022, 2023, 2023, 2023), Categories = c("Football", 
"Basket", "Volley", "Football", "Basket", "Volley", "Football", 
"Basket", "Volley", "Football", "Basket", "Volley", "Tennis", 
"Hockey", "Other", "Tennis", "Hockey", "Other", "Tennis", "Hockey", 
"Other", "Tennis", "Hockey", "Other"), Satisfaction = c(46.7, 
20.7, 36.5, 43.7, 21, 31.2, 49, 17.9, 34.2, 33.8, 30.1, 33.2, 
68.7, 68.7, 68.7, 69.7, 70.2, 70.2, 66.8, 72.7, 72.7, 76.1, 75.4, 
75.4)), row.names = c(NA, -24L), class = c("tbl_df", "tbl", "data.frame"
))

我用ggplot2绘制了分组分面折线图,原代码如下:

P2 = ggplot(df, aes(x = Year, y = Satisfaction, color = factor(Categories), group = Categories)) +
  geom_line(aes(color = ifelse(answers == "Unfavorable", "darkred", "forestgreen"))) +
  geom_point(size = 2) +
  facet_grid(Categories ~ answers, scales = "free_y") +
  theme_minimal() +
  labs(
    x = "Year",
    y = "Satisfaction",
    color = "Categories") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        strip.background = element_rect(fill = "lightgrey"), # Add background color to facet labels
        strip.text = element_text(size = 12, face = "bold"), # Enhance facet label text
        panel.spacing = unit(1, "lines")) + # Increase spacing between facets
  geom_text(aes(label = paste(Satisfaction,"%") ), nudge_x = 0.1,color="black")+ 
  theme(legend.position = "none")
P2

现在需要将answers为"A"组的折线设置为绿色,"B"组的折线设置为红色,请问如何实现?


解决方案

直接修正颜色映射的逻辑,绑定answers字段并手动指定对应颜色即可,修正后的完整代码如下:

P2 = ggplot(df, aes(x = Year, y = Satisfaction, group = Categories)) +
  # 按answers分组指定颜色
  geom_line(aes(color = answers)) +
  geom_point(size = 2, aes(color = answers)) +
  facet_grid(Categories ~ answers, scales = "free_y") +
  theme_minimal() +
  labs(
    x = "Year",
    y = "Satisfaction",
    color = "Answer Group") +
  # 手动指定颜色:A组森林绿,B组深红
  scale_color_manual(values = c("A" = "forestgreen", "B" = "darkred")) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        strip.background = element_rect(fill = "lightgrey"),
        strip.text = element_text(size = 12, face = "bold"),
        panel.spacing = unit(1, "lines")) +
  geom_text(aes(label = paste(Satisfaction,"%") ), nudge_x = 0.1, color="black") +
  theme(legend.position = "none")
P2

关键修改说明

  1. 移除原主aes中错误的color = factor(Categories),改为按answers绑定颜色映射
  2. 修正原代码中ifelse的错误判断条件(原用了不存在的"Unfavorable"值),直接通过scale_color_manual明确指定A/B组的对应颜色
  3. 同步给geom_point添加颜色映射,保证折线和点的颜色统一

内容的提问来源于stack exchange,提问作者Homer Jay Simpson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 14:07:32