Home > Software engineering >  R, ggplot2 - Change background by value and day
R, ggplot2 - Change background by value and day

Time:06-14

I have an hourly data frame with three variables: Obs, Model (grouped in "id") and Clouds, i want to compare the observations with the model but i want to color my background according to cloudiness. Clouds is a daily value (it repeats itself every hour of the day).

data = pivot_longer(data, cols = 2:3, names_to = 'id', values_to = 'var') %>% drop_na()

g = ggplot(data, aes(x = Time, y = var, group = id))  
  geom_rect(aes(xmin = Time, xmax = lead(Time), ymin = -Inf, ymax = Inf,
                color = Clouds), size = 2)  
  geom_line()

Which results in this

Which is not correct according to the data. Any help?

CodePudding user response:

You should try fill=Clouds, along with alpha = 0.5 outside of the aes().

I'm assuming you've already found this, but I'll leave this here, since it's a similar problem.

  • Related