代码之家  ›  专栏  ›  技术社区  ›  Homer Jay Simpson

根据类别更改R中ggplot2中线条的颜色

  •  0
  • Homer Jay Simpson  · 技术社区  · 10 月前

    R中有以下数据帧

    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
    

    其在R中的结构如下:

    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对绘图进行了分组,如下图所示。但我想改变线条的颜色。如果是A组,线条是绿色的,如果是B组是红色的。我该如何在R中做到这一点?

    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))+
      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
    
    

    enter image description here

    2 回复  |  直到 10 月前
        1
  •  2
  •   stefan    10 月前

    如果不需要图例,您可以通过包裹 if_else 用于有条件地指定颜色 I() 然而,这需要 ggplot2 >= 3.5.0 。这样做,您仍然可以在 color 至于你的分数,不需要指定单独的色阶。

    library(ggplot2)
    
    packageVersion("ggplot2")
    #> [1] '3.5.1'
    
    ggplot(df, aes(
      x = Year, y = Satisfaction,
      color = factor(Categories), group = Categories
    )) +
      geom_line(
        aes(color = I(ifelse(answers == "A", "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"),
        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")
    

    更新 如果你想让这些点的颜色相同,那么我建议你进行全球地图绘制 answers 颜色 aes,并按照@YacineHajji的回答中建议的手动比例设置颜色。(或使用 aes(color = I(ifelse(answers == "A", "darkred", "forestgreen"))) 对于 geom_point 也)。如果你喜欢所有黑点,那么使用 geom_point(size = 2, color = "black") .

    ggplot(df, aes(
      x = Year, y = Satisfaction,
      color = answers
    )) +
      geom_line() +
      geom_point(size = 2) +
      scale_color_manual(
        values = c(A = "darkred", B = "forestgreen"),
        guide = "none"
      ) +
      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"),
        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")
    

        2
  •  2
  •   Yacine Hajji    10 月前

    您可以在中指定颜色所依赖的变量 geom_line 这里是“答案”。

    然后指定 scale_colour_manual 根据你的“答案”组值定义你的颜色,这里是A或B。

    ggplot(df, aes(x = Year, y = Satisfaction, color = factor(Categories), group = Categories)) +
      geom_line(aes(colour=answers)) +
      scale_colour_manual(values=c(A="darkred", B="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")
    

    enter image description here