【问题标题】:Year by year comparison of observed and simulated data using facet_wrap functionality of R?使用 R 的 facet_wrap 功能逐年比较观察数据和模拟数据?
【发布时间】:2021-10-04 11:13:55
【问题描述】:

我有以下data.frame

library(lubridate)
library(tidyverse)

set.seed(123)

DF <- data.frame(Date = seq(as.Date("2001-01-01"), to = as.Date("2005-12-31"), by = "day"),
                 Sim = runif(1826,1,5), Obs = runif(1826,3,7)) %>% 
  separate(Date, into = c("Year", "Month","Day")) %>% 
  mutate(Sim_Vol = Sim*86400, Obs_Vol = Obs*86400 ) %>% 
  filter(between(Month, 3,5))

我想逐年使用ggplotfacet_wrap 功能为每个Year 找到Sim_VolObs_Vol 中的total sum,然后是bar plotYearly total data比较。 附上一个示例图以进一步澄清

【问题讨论】:

    标签: r dataframe ggplot2 dplyr tidyverse


    【解决方案1】:

    这样的?

    DF %>%
      group_by(Year) %>%
      summarise(across(c(Sim_Vol, Obs_Vol), sum)) %>%
      pivot_longer(cols = ends_with("Vol")) %>%
      ggplot(aes(x = name)) +
      geom_bar(aes(weight = value)) +
      facet_wrap(~Year)
    

    【讨论】:

      猜你喜欢
      • 2020-01-14
      • 2021-12-08
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      • 1970-01-01
      相关资源
      最近更新 更多