Search code examples
rggplot2

How to plot temperature data with low and high geom_line?


Following this question (How to use average value in geom_line?), how can I change this graph with two geom_lines: one representing the averaged low temperatures from 20:00 to 08:00 and the other representing the averaged high temperatures from 08:01 to 19:59?

The one month data link is there in the above link.

However, below is the two days weather data.

structure(list(Rain = c(0, 0, 0, 0, 0, 2, 0, 5, 0, 0, 0, 0, 5, 
0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.2, 0, 0.2, 0, 
0.2, 0.2, 0, 0.2, 0.2, 0, 0.2, 0, 0.2, 0, 0.2, 0.2, 0.2, 0.2, 
0, 0.2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.4, 0.4, 0, 0, 0, 0, 0, 
0.2, 0.2, 0, 0.2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0.2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0), Temperature = c(10.201, 9.923, 9.858, 9.751, 9.794, 9.579, 
10.995, 11.252, 11.403, 11.574, 11.917, 12.046, 12.175, 12.153, 
12.346, 12.689, 13.161, 13.268, 13.504, 13.633, 14.019, 14.234, 
14.534, 14.641, 14.856, 15.07, 15.092, 15.628, 15.649, 15.842, 
16.078, 16.807, 16.807, 16.743, 17.344, 17.322, 17.558, 18.159, 
18.588, 18.974, 18.823, 18.952, 19.059, 19.681, 19.939, 20.067, 
20.218, 20.153, 20.754, 20.454, 20.754, 20.925, 21.097, 21.504, 
21.462, 21.633, 21.698, 21.569, 21.976, 21.612, 21.569, 21.805, 
21.74, 22.019, 21.955, 22.212, 22.105, 22.47, 22.298, 22.126, 
22.727, 22.491, 22.598, 22.513, 22.748, 22.427, 22.555, 22.598, 
22.92, 22.706, 22.899, 23.049, 22.727, 22.92, 22.963, 23.135, 
22.856, 23.092, 23.113, 23.285, 23.263, 23.22, 23.285, 23.285, 
23.006, 23.092, 23.156, 22.834, 22.748, 22.727, 22.577, 22.47, 
22.319, 22.169, 22.019, 17.043, 17.73, 18.03, 17.901, 17.837, 
17.472, 17.408, 17.258, 17.193, 16.915, 16.786, 16.7, 16.486, 
16.25, 16.014, 15.714, 15.885, 15.628, 15.37, 15.628, 15.671, 
15.606, 15.521, 15.671, 15.628, 15.156, 15.177, 15.092, 14.899, 
14.727, 14.92, 15.22, 15.327, 15.649, 15.671, 15.585, 15.327, 
15.22, 15.37, 15.521, 15.756, 16.035, 15.778, 15.499, 14.705, 
14.341, 13.955, 13.29, 13.033, 12.754, 12.668, 12.604, 12.582, 
12.561, 12.453, 12.196, 12.132, 12.175, 12.239, 12.346, 12.411, 
12.411, 12.175, 12.025, 11.767, 11.681, 11.596, 11.553, 11.488, 
11.488, 11.488, 11.424, 11.36, 11.252, 11.338, 11.317, 11.295, 
11.231, 11.295, 11.124, 10.974, 10.952, 10.909, 10.888, 10.952, 
12.26, 12.346, 12.346, 12.196, 12.411, 12.561, 13.247, 13.655, 
14.083, 16.293, 16.722, 16.314, 17.022, 16.958, 16.979, 16.979, 
17.193, 16.722, 16.571, 16.1, 15.521, 14.663, 14.405, 14.577, 
14.834, 15.027, 15.306, 15.563, 15.692, 15.499, 15.22, 15.242, 
14.92, 14.856, 14.684, 14.448, 14.277, 14.191, 13.869, 13.719, 
10.63, 10.695, 10.673, 10.523, 10.459, 10.352, 10.18, 10.116, 
10.073, 10.073, 9.923, 9.966, 9.923, 10.137, 10.073, 9.944, 9.88, 
9.966, 10.03, 9.966, 9.987, 9.88), Date = structure(c(-710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710610, -710610, -710610, -710610, 
-710610, -710610, -710610, -710609, -710609, -710609, -710609, 
-710609, -710609, -710609, -710609, -710609, -710609, -710609, 
-710609, -710609, -710609, -710609, -710609, -710609, -710609, 
-710609, -710609, -710609, -710609, -710609, -710609, -710609, 
-710609, -710609, -710609, -710609, -710609, -710609, -710609, 
-710609, -710609, -710609, -710609, -710609, -710609, -710609, 
-710609, -710609, -710609, -710609, -710609, -710609, -710609, 
-710609, -710609, -710609, -710609, -710609, -710609, -710609, 
-710609, -710609, -710609, -710609, -710609, -710609, -710609, 
-710609, -710609, -710609, -710609, -710609, -710609, -710609, 
-710609, -710609, -710609, -710609, -710609, -710609, -710609, 
-710609, -710609, -710609, -710609, -710609, -710609, -710609, 
-710609, -710609, -710609, -710609, -710609, -710609, -710609, 
-710609, -710609, -710609, -710609, -710609, -710609, -710609, 
-710609, -710609, -710609, -710609, -710609, -710609, -710609, 
-710609, -710609, -710609, -710609, -710609, -710609, -710609, 
-710609, -710609, -710609, -710609, -710609, -710609, -710609, 
-710609, -710609, -710609, -710609, -710609, -710609), class = "Date"), 
    Time = c("00:00:00", "00:05:00", "00:10:00", "00:15:00", 
    "00:20:00", "00:25:00", "08:15:00", "08:20:00", "08:25:00", 
    "08:30:00", "08:35:00", "08:40:00", "08:45:00", "08:50:00", 
    "08:55:00", "09:00:00", "09:05:00", "09:10:00", "09:15:00", 
    "09:20:00", "09:25:00", "09:30:00", "09:35:00", "09:40:00", 
    "09:45:00", "09:50:00", "09:55:00", "10:00:00", "10:05:00", 
    "10:10:00", "10:15:00", "10:20:00", "10:25:00", "10:30:00", 
    "10:35:00", "10:40:00", "10:45:00", "10:50:00", "10:55:00", 
    "11:00:00", "11:05:00", "11:10:00", "11:15:00", "11:20:00", 
    "11:25:00", "11:30:00", "11:35:00", "11:40:00", "11:45:00", 
    "11:50:00", "11:55:00", "12:00:00", "12:05:00", "12:10:00", 
    "12:15:00", "12:20:00", "12:25:00", "12:30:00", "12:35:00", 
    "12:40:00", "12:45:00", "12:50:00", "12:55:00", "13:00:00", 
    "13:05:00", "13:10:00", "13:15:00", "13:20:00", "13:25:00", 
    "13:30:00", "13:35:00", "13:40:00", "13:45:00", "13:50:00", 
    "13:55:00", "14:00:00", "14:05:00", "14:10:00", "14:15:00", 
    "14:20:00", "14:25:00", "14:30:00", "14:35:00", "14:40:00", 
    "14:45:00", "14:50:00", "14:55:00", "15:00:00", "15:05:00", 
    "15:10:00", "15:15:00", "15:20:00", "15:25:00", "15:30:00", 
    "15:35:00", "15:40:00", "15:45:00", "15:50:00", "15:55:00", 
    "16:00:00", "16:05:00", "16:10:00", "16:15:00", "16:20:00", 
    "16:25:00", "18:45:00", "18:50:00", "18:55:00", "19:00:00", 
    "19:05:00", "19:10:00", "19:15:00", "19:20:00", "19:25:00", 
    "19:30:00", "19:35:00", "19:40:00", "19:45:00", "19:50:00", 
    "19:55:00", "20:00:00", "20:05:00", "20:10:00", "20:15:00", 
    "20:20:00", "20:25:00", "20:30:00", "20:35:00", "20:40:00", 
    "20:45:00", "02:25:00", "02:30:00", "02:35:00", "02:40:00", 
    "02:45:00", "02:50:00", "02:55:00", "03:00:00", "03:05:00", 
    "03:10:00", "03:15:00", "03:20:00", "03:25:00", "03:30:00", 
    "03:35:00", "03:40:00", "03:45:00", "03:50:00", "03:55:00", 
    "04:00:00", "04:05:00", "04:10:00", "04:15:00", "04:20:00", 
    "04:25:00", "04:30:00", "04:35:00", "04:40:00", "04:45:00", 
    "04:50:00", "04:55:00", "05:00:00", "05:05:00", "05:10:00", 
    "05:15:00", "05:20:00", "05:25:00", "05:30:00", "05:35:00", 
    "05:40:00", "05:45:00", "05:50:00", "05:55:00", "06:00:00", 
    "06:05:00", "06:10:00", "06:15:00", "06:20:00", "06:25:00", 
    "06:30:00", "06:35:00", "06:40:00", "06:45:00", "06:50:00", 
    "06:55:00", "07:00:00", "07:05:00", "07:10:00", "07:15:00", 
    "07:20:00", "09:30:00", "09:35:00", "09:40:00", "09:45:00", 
    "09:50:00", "09:55:00", "10:00:00", "10:05:00", "10:10:00", 
    "14:40:00", "14:45:00", "14:50:00", "14:55:00", "15:00:00", 
    "15:05:00", "15:10:00", "15:15:00", "15:20:00", "15:25:00", 
    "15:30:00", "15:35:00", "15:40:00", "15:45:00", "15:50:00", 
    "15:55:00", "16:00:00", "16:05:00", "16:10:00", "16:15:00", 
    "16:20:00", "16:25:00", "16:30:00", "16:35:00", "16:40:00", 
    "16:45:00", "16:50:00", "16:55:00", "17:00:00", "17:05:00", 
    "17:10:00", "20:30:00", "20:35:00", "20:40:00", "20:45:00", 
    "20:50:00", "20:55:00", "21:00:00", "21:05:00", "21:10:00", 
    "21:15:00", "21:20:00", "21:25:00", "21:30:00", "21:35:00", 
    "21:40:00", "21:45:00", "21:50:00", "21:55:00", "22:00:00", 
    "22:05:00", "23:50:00", "23:55:00")), row.names = c(NA, -252L
), class = "data.frame")

Solution

  • One way is to create a new variable based on the time and then use that as the col argument to geom_line.

    library(ggplot2)
    library(hms)
    library(dplyr)
    
    df |>
      mutate(Date=as.Date(Date, format="%m/%d/%Y"),
             t=as_hms(Time),
             Hi_lo=factor(
               t>as_hms("08:00:00") & t<=as_hms("20:00:00"),
               levels=c(TRUE,FALSE),
               labels=c("High","Low"))) |>
      summarise(Rain=sum(Rain), 
                Temperature=mean(Temperature),
                .by=c(Date, Hi_lo)) |>
      ggplot(aes(Date, Rain)) +  # Need to specify group
      geom_line(aes(y = Temperature, col=Hi_lo), linewidth = 1.5) + 
      scale_x_date(guide = guide_axis(angle = 90),
                   breaks="day") +
      scale_y_continuous("Precipitation", 
                         sec.axis = sec_axis(~., name = "Temperature")) +
      geom_col() +
      theme_minimal() 
    

    enter image description here