Home > Mobile >  Plot daily cumulative and daily hourly precipitation in same graph
Plot daily cumulative and daily hourly precipitation in same graph

Time:11-04

I have data like this:

datetime aws.pr_sum aws.ta_2m timestamp
2020-06-14 12:00:00 0.00 19.20000 1635171717
2020-06-14 13:00:00 0.85 19.73833 1635171717
2020-06-14 14:00:00 0.00 21.95667 1635171717
2020-06-14 15:00:00 0.00 21.71333 1635171717
2020-06-14 16:00:00 0.00 21.04667 1635171717
2020-06-14 17:00:00 0.00 20.66500 1635171717

and as the title says i want to plot daily cumulative and daily hourly precipitation in same graph. What i do:


df <- df  %>% group_by(timestamp) %>%
                    mutate(aws_pcp_cumulative = cumsum(aws.pr_sum)) 
c3 <- c("inca" = "#6aa6fa", "AWS" = "#2ECBE9")

p3 = ggplot(df, aes(x=datetime))  
geom_bar(stat = "identity", aes(x = datetime   shift/2, y = aws.pr_sum, color="AWS"), size=0.7, fill=NA, width=wd )  
geom_line(data = df, aes(x = datetime, y = aws_pcp_cumulative, color =  "AWS"), size=1.2,alpha=0.2)  
geom_bar(stat = "identity", aes(x = datetime - shift/2, y = inca.RR.max, color="inca"), size=0.7, fill=NA, width=wd)  
geom_line(data = df, aes(x = datetime, y = inca_pcp_cumulative, color = "inca"), size=1.2,alpha=0.2)  
geom_text(aes(label=laws, x = datetime, y = aws.pr_sum, color="AWS"), vjust = -0.5, size=3)  
geom_text(aes(label=linca, x = datetime, y = inca.RR.max, color="inca"), vjust = -0.5, size=3)  
scale_x_datetime(breaks=brks, labels = lbls, date_labels="%d.%m %H:%M")  
  labs(x="",
       y= "",
       color="zrážky [mm/h]")  
  scale_color_manual(values = c3)  
  theme(
    panel.grid.major.x = element_blank(),
    legend.position = c(0.01,0.95),
    legend.justification = c("left", "top"),
    legend.box.just = "left",
    legend.margin = margin(6, 30, 6,6),
    legend.title = element_text(size = 12))  
 

Here is results : https://imgur.com/a/MYmJhy7

As we can see, a cumulative sum is made for the whole period, but I want it per day. Any ideas ?

CodePudding user response:

Unfortunately, all of your time points were from the same date. You can get the cumulative value by using dplyr::group_by and sum:

library(tidyverse)

data <-
  tribble(
    ~datetime, ~aws.pr_sum, ~aws.ta_2m, ~timestamp,
    "2020-06-14 12:00:00", 0, 19.2, 1635171717L,
    "2020-06-14 13:00:00", 0.85, 19.73833, 1635171717L,
    "2020-06-14 14:00:00", 0, 21.95667, 1635171717L,
    "2020-06-14 15:00:00", 0, 21.71333, 1635171717L,
    "2020-06-14 16:00:00", 0, 21.04667, 1635171717L,
    "2020-06-14 17:00:00", 0, 20.665, 1635171717L
  )
data
#> # A tibble: 6 x 4
#>   datetime            aws.pr_sum aws.ta_2m  timestamp
#>   <chr>                    <dbl>     <dbl>      <int>
#> 1 2020-06-14 12:00:00       0         19.2 1635171717
#> 2 2020-06-14 13:00:00       0.85      19.7 1635171717
#> 3 2020-06-14 14:00:00       0         22.0 1635171717
#> 4 2020-06-14 15:00:00       0         21.7 1635171717
#> 5 2020-06-14 16:00:00       0         21.0 1635171717
#> 6 2020-06-14 17:00:00       0         20.7 1635171717

data %>%
  mutate(
    datetime = datetime %>% as.POSIXct(),
    day = datetime %>% as.Date()
  ) %>%
  group_by(day) %>%
  mutate(
    aws.pr_su.day = sum(aws.pr_sum)
  ) %>%
  pivot_longer(c(aws.pr_sum, aws.pr_su.day)) %>%
  qplot(datetime, value, color = name, data = .)

Created on 2021-10-27 by the reprex package (v2.0.1)

  • Related