【问题标题】:Plot daily cumulative and daily hourly precipitation in same graph在同一图中绘制每日累积降水量和每日每小时降水量
【发布时间】:2021-10-27 11:01:15
【问题描述】:

我有这样的数据:

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

正如标题所说,我想在同一张图中绘制每日累积降水量和每日每小时降水量。我做什么:


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))  
 

这是结果:https://imgur.com/a/MYmJhy7

正如我们所见,整个期间都会累积总和,但我希望每天计算一次。有什么想法吗?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    很遗憾,您的所有时间点都来自同一日期。 使用dplyr::group_bysum可以得到累计值:

    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 = .)
    

    reprex package (v2.0.1) 于 2021 年 10 月 27 日创建

    【讨论】:

    • 谢谢,这个例子帮助我找到了解决方案。
    猜你喜欢
    • 2020-05-06
    • 2014-05-11
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 2014-11-27
    • 1970-01-01
    相关资源
    最近更新 更多