【问题标题】:Shift scale at date x-axis with a non-continuous sequence of time in R在日期 x 轴上移动刻度,在 R 中具有非连续的时间序列
【发布时间】:2019-09-23 11:13:30
【问题描述】:

我有一个从 2000 年到 2018 年的时间序列数据,从每年 1 月到 12 月。我想将 2000-2016 年期间 temp_mean 的平均值与 2017-2018 年观察到的 temp_mean 的线重叠。

我可以用一个非常手工制作的代码来完成它,但是我意识到它可以用非常简单的方式完成,现在可以使用用于管理绘图日期的包(包“scales”、“lubridate”等)

有人可以帮助我简化我的工作以完成生成的情节吗?我会很感激的。

suppressWarnings(suppressMessages(library("tidyverse", quietly = T)))

dat <- tibble(
  date = seq(as.Date("2010-01-01"), as.Date("2018-12-31"), by=1),
  tmean = rgamma(length(date), shape=2, scale=2)) %>%
  mutate(year = lubridate::year(date),
         month = lubridate::month(date),
         julian = lubridate::yday(date))

# calculate daily average temperature for the serie 2010-2016
dat_serie <- dat %>%
  filter(year<2017) %>%
  group_by(julian) %>%
  summarise(
    month = first(month),
    avg = mean(tmean, .2, na.rm = T)) %>%
  ungroup()


p0 <- dat_serie %>%
  filter(month %in% c("1", "2", "3", "9","10", "11", "12")) %>%
  mutate(julian_AWS = ifelse(julian>=244, julian-243, julian+123)) %>%
  ggplot() +
  geom_line(aes(julian_AWS, avg, color = factor(month)))+

  scale_x_continuous(breaks = c(1,#S
                                31,#O
                                61,#N
                                91,#D
                                121,#E
                                151,#F
                                181),#M
                     labels = c("Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar"))+
  theme(axis.text.x=element_text(hjust=-1))

我主要关心的是如何匹配意甲和 2017-2018 年的 x 轴值以重叠 y 值。在here 的帮助下,我添加了 2017-2018 行:

dat_17_18 <-  dat %>% 
filter(date >= '2017-09-01', date < '2018-03-30') %>% 
mutate(julian_AWS = ifelse(julian>=244, julian-243, julian+123)) 

p0 + geom_line(data = dat_17_18,
            aes(julian_AWS, tmean, linetype = factor(year)))

reprex package (v0.2.1) 于 2019 年 5 月 5 日创建

【问题讨论】:

    标签: r date ggplot2


    【解决方案1】:

    你很亲密。如果我理解正确,您可以将汇总数据加入 2017-2018 年数据并继续使用原始数据集中的日期

    dat_17_18 <-
      dat %>%
      filter(date >= '2017-09-01', date < '2018-03-30') %>%
      left_join(dat_serie, by = c("juian", "month"))
    
    ggplot(dat_17_18, aes(x = date)) +
      geom_line(aes(y = avg, color = factor(month))) +
      geom_line(aes(y = tmean, linetype = factor(year))) +
      scale_x_date(date_breaks = "1 month", date_labels = "%b") +
      theme(axis.text.x = element_text(hjust = -1))
    
    

    此外,由于我们处于此图表的核心部分,因此将底图设为面积图可能比 2 条锯齿线更易于阅读。

    ggplot(dat_17_18, aes(x = date)) +
      geom_area(
        aes(y = avg, fill = factor(month)), 
        show.legend = FALSE, alpha = 0.5
      ) +
      geom_line(aes(y = tmean, linetype = factor(year))) +
      scale_x_date(date_breaks = "1 month", date_labels = "%b") +
      theme(axis.text.x = element_text(hjust = -1))
    

    【讨论】:

    • 你知道如何从x轴上去掉“april”吗?
    • 你可以使用scale_x_date(..., expand = expand_scale(0, 0))
    • 你完成了我的一天!
    猜你喜欢
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 2016-09-26
    • 2015-08-13
    相关资源
    最近更新 更多