1

I'm plotting graphs in R from blood pressure measurements stored in Apple's Apple health app following the fantastic blog from Taras Kaduk.

I changed the code for my needs and now I would like to change the background colours from the plot depending on the time value. I have added a column daynight with the two values "night" and "day" ("night"= between 18:00 and 06:00, "day" from 06:00 until 18:00). Is there a way to use this values to switch the background color in the graph, so that all the night periods are darkened? Alternatively I could use directly the datetime value (x axis), that is a POSIXct value.

I did only find examples that do the trick but with one (or a few) fixed date values like this one: Change color background in ggplot2 - R by specific Date on x axis.

Here a dataset and the code I use so far:

Sample data (blood_pressure):
  value      type            datetime daynight         state
1    96 Diastolic 2022-01-10 07:52:48      day Hypertension 1
2   102 Diastolic 2022-01-10 07:09:58      day Hypertension 2
3   109 Diastolic 2022-01-09 19:58:56    night Hypertension 2
4   141  Systolic 2022-01-09 08:27:24      day Hypertension 1
5   146  Systolic 2022-01-10 19:09:19    night Hypertension 1


blood_pressure  %>% 
  ggplot(blood_pressure, aes(datetime, value)) + 
  geom_point()

Here You can see an example plot:

enter image description here

geom
  • 403
  • 1
  • 3
  • 17
  • 8
    Please remove all packages and all data preparation steps not strictly necessary to illustrate your issue. We only need a very simple data set to play with, and a very basic plot with some date-time values on the x axis to which the background can be added. E.g. `d = data.frame(x = seq(from = Sys.time(), by = "1 hour", len = 50), y = 1)`; `ggplot(d, aes(x, y)) + geom_point()`. The rest is just noise. – Henrik Jan 10 '22 at 21:02
  • 1
    Please add an example of your data, preferably with `dput()` – utubun Jan 10 '22 at 21:02
  • 1
    [Shade ggplot2 background according to factor level](https://stackoverflow.com/questions/38535563/shade-ggplot2-background-according-to-factor-level); "different shading for levels in the factor 'Light'. So all points with 'nightime' in 'Light' would be shaded in grey while the 'daytime' would be white." – Henrik Jan 10 '22 at 21:16
  • [Make the background of a graph different colours in different regions](https://stackoverflow.com/questions/9968975/make-the-background-of-a-graph-different-colours-in-different-regions), i.e. one possibility is to use `seq` to create a sequence of POSIXct to pass to `geom_segment` – Henrik Jan 10 '22 at 21:18
  • I'll have a look at both examples and see if and how to get that work. Thanks a lot. – geom Jan 10 '22 at 22:16

1 Answers1

2

Here's an example using @Henrik's data:

d = data.frame(x = seq(from = Sys.time(), by = "1 hour", len = 50), y = 1)
d$hour = as.numeric(format(d$x, "%H"))
d$hour_shade =ifelse(d$hour >= 18 | d$hour <= 6, "gray60", "gray90")
ggplot(d, aes(x, y)) + 
  geom_rect(aes(xmin = x, xmax = lead(x), ymin = -Inf, ymax = Inf,
                fill = hour_shade)) +
  geom_point() +
  scale_fill_identity()

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53