Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change colour of line in ggplot2 in R based on a category

i have the following data frame in 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

which its structure in R is the following:

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

i have grouped the plot using ggplot2 as follows and as the picture shows. But I want to change the colours of the lines.If it the A group the line to be green and if is group B to be red. How can i do it in R ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

If a legend is not required you can easily achieve your desired result by wrapping the if_else you use to conditionally assign the color in I() aka AsIs which however requires ggplot2 >= 3.5.0. Doing so you can still map a different variable on the color as for your points without the need to specify a separate color scale.

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading