Home > Mobile >  Creating a ggplot where the color of point/line is dependent on the value in another column? Multipl
Creating a ggplot where the color of point/line is dependent on the value in another column? Multipl

Time:09-16

I have a set of data like below. What I want to do is plot date vs values and then have the color of the point/line depend on the X value.

I want to be able to create categories of different colors. So for example the data below, I have plotted date vs time then I would like colour to be dependent on the X value i.e., anything under 3 to display BLUE, anything above 3 and below 6 to be RED and anything above 6 to be GREEN.

I know how to create a graph which changes the color once depending on the limit I set using the code below but want to create colour categories based on the X value and also decide what colours they are.

    date       values       X
2021-08-09       1          0.5
2021-08-09       8          0.7
2021-08-09       6          2
2021-08-09       14         3
2021-08-09       18         5
2021-08-09       10         6
2021-08-09       1          4
2021-08-09       3          9
2021-08-09       6          0.3
2021-08-09       8          0.9
2021-08-09       11         4


    ggplot(df)   geom_line(aes(x = date, y = values, colour = X > 5)) 

Code now looks like this? Why is no category for 10 showing up on the graph even though there are values of X = 10

    All_Hourly_1 %>%
  mutate(group = case_when(
    max_AQHI < 2 ~ 1,
    max_AQHI < 3 ~ 2,
    max_AQHI < 4 ~ 3,
    max_AQHI < 5 ~ 4,
    max_AQHI < 6 ~ 5,
    max_AQHI < 7 ~ 6,
    max_AQHI < 8 ~ 7,
    max_AQHI < 9 ~ 8,
    max_AQHI < 10 ~ 9,
    TRUE ~ 10)) %>%
      ggplot(aes(x = date, y = PM_RAMP, color = as.factor(group), group = 1))  
  geom_line()   scale_color_manual(values = c("deepskyblue1", "dodgerblue", "dodgerblue4", "yellow", "gold", "goldenrod", "lightcoral","red", "red3", "red4"), labels = c("1 (Low Risk)","2 (Low Risk)","3 (Low Risk)","4 (Moderate Risk)","5 (Moderate Risk)","6 (Moderate Risk)","7 (High Risk)","8 (High Risk)","9 (High Risk)","10  (Very High Risk)"))   labs(colour = "Air Quality Health Index Value")   geom_line(size = 1)   labs (x = "Date", y = expression(PM[2.5]," (µg ", m^-3,")"))

But the graph looks like below: enter image description here

CodePudding user response:

Try case_when before ggplot and then use scale_color_manual to set colors and it's better to change date as date format using as.Date.

df %>%
  mutate(group = case_when(
    X < 3 ~ 1,
    X < 6 ~ 2,
    TRUE ~ 3
  ), date = as.Date(date)
  ) %>%
  ggplot(aes(x = date, y = values, color = as.factor(group)))  
  geom_line()   scale_color_manual(values = c("blue", "red", "green")) 

CodePudding user response:

Data

df <- structure(list(date = c("2021-08-09", "2021-08-10", "2021-08-11", 
"2021-08-12", "2021-08-13", "2021-08-14", "2021-08-15", "2021-08-16", 
"2021-08-17", "2021-08-18", "2021-08-19"), values = c(1L, 8L, 
6L, 14L, 18L, 10L, 1L, 3L, 6L, 8L, 11L), X = c(0.5, 0.7, 2, 3, 
5, 6, 4, 9, 0.3, 0.9, 4)), class = "data.frame", row.names = c(NA, 
-11L))

How to

library(tidyverse)

df %>%
  mutate(color = cut(X,c(0,2,5,Inf),labels = c("<3","<6","6 "))) %>%
  ggplot(aes(x = date, y = values, color = color, group = 1))  
  geom_line() 
  scale_color_manual(values = c("blue", "red", "green")) 

Result

enter image description here

  • Related