如何制作 4 年内 3 个月日期的时间序列,但仅限于 Mar-Abr-May 个讲座

How to make time series with date for 3 months in 4 years but only in Mar-Abr-May lectures

我正在做时间序列。我只有 3 个月的数据,每年的每一天 我只希望评估的月份出现在 x-axis(三月、四月、五月)

数据头

脚本 1:

OZAS$date=as.Date(OZAS$date)
ST_2016 <- ggplot(OZAS, aes(x = date, y = oz)) +
  geom_line() +
  geom_hline(yintercept = 80, color = "red", linetype = "dashed", size = 1) +
  scale_x_date()+
  scale_y_continuous(limits = c(0,150)) +
  labs(x = "Meses", y = bquote('Concentración (mu g/m^3)'))+
  facet_wrap(~ station, nrow=4) +
  theme_cowplot(12) +
  panel_border() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.text=element_text(size=10), axis.title=element_text(size=12), 
        strip.background = element_rect(colour="black", fill="white",linetype="solid"),
        strip.text.x = element_text(size=12, color="black", face="bold"))
  ST_2016

脚本 2:

ST_2016 <- ggplot(OZAS, aes(x = date, y = oz)) +
  geom_line() +
  geom_hline(yintercept = 80, color = "red", linetype = "dashed", size = 1) +
  scale_x_date(breaks = date_breaks("month"), labels = date_format("%b"))+
  scale_y_continuous(limits = c(0,150)) +
  labs(x = "Meses", y = bquote('Concentración (mu g/m^3)'))+
  facet_wrap(~ station, nrow=4) +
  theme_cowplot(12) +
  panel_border() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.text=element_text(size=10), axis.title=element_text(size=12), 
        strip.background = element_rect(colour="black", fill="white",linetype="solid"),
        strip.text.x = element_text(size=12, color="black", face="bold"))
  ST_2016

评论: 我知道问题出在 scale_x_date 但我不知道如何修改它,以便在所有年份中只出现评估的月份,并且每个年份都相同。

谢谢你的帮助。

数据 我希望我使用好的dput,是我第一次(如果不是,我可以纠正它)。

structure(list(date = structure(c(1488326400, 1488412800, 1488499200, 
1488585600, 1488672000, 1488758400, 1488844800, 1488931200, 1489017600, 
1489104000, 1489190400, 1489276800, 1489363200, 1489449600, 1489536000, 
1489622400, 1489708800, 1489795200, 1489881600, 1489968000, 1490054400, 
1490140800, 1490227200, 1490313600, 1490400000, 1490486400, 1490572800, 
1490659200, 1490745600, 1490832000, 1490918400, 1491004800, 1491091200, 
1491177600, 1491264000, 1496016000, 1496102400, 1496188800, 1519862400, 
1519948800, 1520035200, 1520121600, 1520208000, 1520294400, 1520380800, 
1520467200, 1520553600, 1520640000, 1520726400, 1520812800, 1520899200, 
1520985600, 1521072000, 1521158400, 1521244800, 1521331200, 1521417600, 
1521504000, 1521590400, 1521676800, 1521763200, 1521849600, 1521936000, 
1522022400, 1522108800, 1522195200, 1527379200, 1527465600, 1527552000, 
1527638400, 1527724800, 1551398400, 1551484800, 1551571200, 1551657600, 
1551744000, 1551830400, 1551916800, 1552003200, 1552089600, 1552176000, 
1552262400, 1552348800, 1552435200, 1552521600, 1552608000, 1552694400, 
1552780800, 1552867200, 1552953600, 1553040000, 1553126400, 1553212800, 
1553299200, 1553385600, 1553472000, 1553558400, 1559174400, 1559260800, 
1583020800, 1583107200, 1583193600, 1583280000, 1583366400, 1583452800, 
1583539200, 1583625600, 1583712000, 1583798400, 1583884800, 1583971200, 
1584057600, 1584144000, 1584230400, 1584316800, 1584403200, 1584489600, 
1584576000, 1584662400, 1584748800, 1584835200, 1584921600, 1585008000, 
1585094400, 1585180800, 1585267200, 1585353600, 1585440000, 1585526400, 
1585612800, 1585699200, 1585785600, 1585872000), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), oz = c(74.888125, 66.61875, 65.0348958333333, 99.7096875, 
78.2740625, 84.5244791666667, 62.3278125, 50.2075, 78.7571875, 
67.1753125, 55.1725, 33.8204166666667, 64.4654166666667, 48.73825, 
43.02125, 85.5305, 57.271, 53.4355, 73.8245, 66.23175, 79.62625, 
58.33575, 66.28875, 52.0596875, 71.429375, 45.365625, 65.821875, 
69.9639583333333, 40.2610625, 35.292625, 58.7643125, 65.0925, 
86.015, 84.60675, 80.690125, 53.6446875, 48.6591666666667, 32.7460416666667, 
64.831875, 39.7108333333333, 72.4845833333333, 64.116125, 54.4107916666667, 
62.5154166666667, 77.1941666666667, 59.1970833333333, 79.9195833333333, 
86.8760416666667, 79.1316666666667, 71.0494166666667, 56.6750416666667, 
69.8744583333333, 57.77075, 90.6685833333333, 69.3689583333333, 
58.50375, 52.7305, 48.9314583333333, 73.5429166666667, 38.1370833333333, 
66.9361666666667, 62.2495833333333, 78.4860416666667, 58.8591666666667, 
54.4730833333333, 56.2415, 36.7683333333333, 24.27125, 45.3735416666667, 
28.460625, 53.7228125, 57.4600625, 71.364375, 58.2522916666667, 
58.01525, 70.8165, 73.5345625, 69.977, 72.730625, 91.3745625, 
45.91275, 68.3404791666667, 42.8676666666667, 69.51125, 39.0605, 
68.15, 80.03975, 65.07325, 38.26075, 19.8046875, 70.755, 49.3938541666667, 
35.348125, 65.8651041666667, 32.225, 65.5341666666667, 56.3072916666667, 
32.00225, 28.991875, 54.7834878834356, 63.5337794018405, 58.5688545552147, 
93.1194966411043, 75.709378297546, 59.3177648006135, 34.8677592791411, 
77.7550582055215, 72.337809892638, 61.3110962423313, 58.0781694785276, 
59.3942426380368, 35.3867452453988, 41.8317411809816, 66.666998696319, 
39.7643857361963, 55.7952855828221, 63.3591481901841, 66.0889980138037, 
33.1872508205521, 70.0908337730061, 66.0577116257669, 70.1359843251534, 
57.0644087116564, 67.4460911042945, 59.3072133128834, 61.273225398773, 
57.6376244171779, 40.3587044171779, 43.9162734662577, 52.3173843558282, 
37.4749582822086, 53.0265424846626, 56.768230904908), station = c("2017", 
"2017", "2017", "2017", "2017", "2017", "2017", "2017", "2017", 
"2017", "2017", "2017", "2017", "2017", "2017", "2017", "2017", 
"2017", "2017", "2017", "2017", "2017", "2017", "2017", "2017", 
"2017", "2017", "2017", "2017", "2017", "2017", "2017", "2017", 
"2017", "2017", "2017", "2017", "2017", "2018", "2018", "2018", 
"2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", 
"2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", 
"2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", 
"2018", "2018", "2018", "2018", "2018", "2018", "2019", "2019", 
"2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", 
"2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", 
"2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", 
"2019", "2019", "2020", "2020", "2020", "2020", "2020", "2020", 
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020", 
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020", 
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020", 
"2020", "2020", "2020", "2020"), day = c("Mie", "Jue", "Vie", 
"Sab", "Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab", "Dom", 
"Lun", "Mar", "Mie", "Jue", "Vie", "Sab", "Dom", "Lun", "Mar", 
"Mie", "Jue", "Vie", "Sab", "Dom", "Lun", "Mar", "Mie", "Jue", 
"Vie", "Sab", "Dom", "Lun", "Mar", "Lun", "Mar", "Mie", "Jue", 
"Vie", "Sab", "Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab", 
"Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab", "Dom", "Lun", 
"Mar", "Mie", "Jue", "Vie", "Sab", "Dom", "Lun", "Mar", "Mie", 
"Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab", "Dom", "Lun", 
"Mar", "Mie", "Jue", "Vie", "Sab", "Dom", "Lun", "Mar", "Mie", 
"Jue", "Vie", "Sab", "Dom", "Lun", "Mar", "Mie", "Jue", "Vie", 
"Sab", "Dom", "Lun", "Mar", "Jue", "Vie", "Dom", "Lun", "Mar", 
"Mie", "Jue", "Vie", "Sab", "Dom", "Lun", "Mar", "Mie", "Jue", 
"Vie", "Sab", "Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab", 
"Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab", "Dom", "Lun", 
"Mar", "Mie", "Jue", "Vie"), month = c("Mar", "Mar", "Mar", "Mar", 
"Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", 
"Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", 
"Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", 
"Abr", "Abr", "Abr", "Abr", "May", "May", "May", "Mar", "Mar", 
"Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", 
"Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", 
"Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "May", 
"May", "May", "May", "May", "Mar", "Mar", "Mar", "Mar", "Mar", 
"Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", 
"Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", 
"Mar", "Mar", "Mar", "May", "May", "Mar", "Mar", "Mar", "Mar", 
"Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", 
"Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", 
"Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", "Mar", 
"Abr", "Abr", "Abr")), row.names = c(NA, -133L), class = c("tbl_df", 
"tbl", "data.frame"))

希望形象

我想获得这样的图像,其中 x 轴是评估的月份。能够比较每年相同月份的行为。

谢谢大家

我认为有必要将所有系列绘制成同一年。 lubridate::yday 为您提供每年的天数,您可以将其转换回日期。

library(ggplot2)
library(cowplot)
library(lubridate)

# toy data
OZAS <- data.frame(
    Fecha = sprintf("%1.0f-%1.0f-%02.0f", 
        rep(2017:2020, each = 14 * 3), 
        rep(3:5, each = 14), 
        1:14), 
    oz = runif(n = 14 * 4 * 3, min = 50, max = 99), 
    station = rep(2017:2020, each = 14 * 3), 
    stringsAsFactors = FALSE)

OZAS$date=as.Date(OZAS$Fecha)
ST_2016 <- ggplot(OZAS, aes(
        x = as.Date(yday(date), origin = "2019-12-31"), 
        y = oz)) +
    geom_line() +
    geom_hline(yintercept = 80, color = "red", linetype = "dashed", size = 1) +
    scale_y_continuous(limits = c(0,150)) +
    labs(x = "Meses", y = bquote('Concentración (\u03BCg/m\u00B3)'))+
    facet_wrap(~ station, nrow = 4) +
    theme_cowplot(12) +
    panel_border() +
    theme(plot.title = element_text(hjust = 0.5),
        axis.text=element_text(size=10), axis.title=element_text(size=12), 
        strip.background = element_rect(colour="black", 
            fill="white",linetype="solid"),
        strip.text.x = element_text(size=12, color="black", face="bold"))
ST_2016

如果您希望折叠 x 轴,您可以尝试将月份绘制为分面。以下是使用上面的新 dput 数据创建的。 lubridate::mday 函数给出一个月中的第几天。

mutate(OZAS, 
    `Dia del Mes` = mday(date), 
    month = factor(month, levels = c("Mar", "Abr", "May"))) %>% 
    ggplot(aes(
        x = `Dia del Mes`, 
        y = oz)) +
    geom_line() +
    scale_x_continuous(breaks = (1:15) * 2 - 1) +
    geom_hline(yintercept = 80, color = "red", linetype = "dashed", size = 1) +
    scale_y_continuous(limits = c(0,150)) +
    labs(y = bquote('Concentración (\u03BCg/m\u00B3)'))+
    facet_grid(station ~ month, 
        space = "free_x", 
        scales = "free_x", 
        shrink = TRUE) +
    theme_cowplot(12) +
    panel_border() +
    theme(plot.title = element_text(hjust = 0.5),
        axis.text=element_text(size=10), axis.title=element_text(size=12), 
        strip.background = element_rect(colour="black", 
            fill="white",linetype="solid"),
        strip.text.y = element_text(size = 12, 
            color = "black", face = "bold"))

Miguel,当您知道问题出在您的 scale_x_date 时,您就差不多成功了。

由于我们没有数据,我使用以下示例创建最近 60 天的日期。

df <- tibble(
      dates = Sys.Date() - 0:59
     ,vals  = runif(60)
)

这给了我 60 个日期和一些随机值。

我可以绘制它并强制 x 刻度显示特定的时间范围。 当我使用简单的 df 时,我的 ggplot 会截断日期 - 所以我强制它更宽。 由于您有一个多年期,ggplot 默认为全年范围。

ggplot(data = df, aes(x = dates, y = vals)) + 
  geom_line() + 
  scale_x_date(date_labels = "%b %d"
              , limits = c( lubridate::ymd("2021-02-01")  # left/start date
                          , lubridate::ymd("2021-06-01")) # right/end date
               )

如果你想“覆盖”年份,你可以创建一个新列并为所有日期分配相同的任意年份(lubridate::year() 允许你设置年份)并有一个进一步的列存储原始年份(例如用于设置颜色)。 这样你就可以再次微调在“同一”年工作的左右边框。

祝你好运!

Miguel,这是一种基于将不同年份“转换”为同一“假”年份的不同方法。为此,我选择了 1999。

OZAS <- OZAS %>%
  mutate( date = as.Date(date)        # for your original plot
         ,dm   = date                 # prepare for day-month
         ,yr   = lubridate::year(dm)  # keep year for colors, etc
         )
lubridate::year(OZAS$dm) <- 1999      # set all timelines to same year

对于情节,我将 x 轴“截断”为您描述的 3 月 1 日至 6 月 1 日(=3 月、4 月、5 月)期间。

ST_2016_b <- ggplot(OZAS, aes(x = dm, y = oz)) +
  geom_line() +
  geom_hline(yintercept = 80, color = "red", linetype = "dashed", size = 1) +
  #scale_x_date()+
  scale_y_continuous(limits = c(0,150)) +
  labs(x = "Meses", y = bquote('Concentración (mu g/m^3)'))+
# --------------------------- truncate presentation to your time horizon  
  scale_x_date(date_labels = "%b %d"
              , limits = c( lubridate::ymd("1999-03-01")  # left/start date
                          , lubridate::ymd("1999-06-01")) # right/end date
               ) +
# ------------------------------------------------------------------------  
  facet_wrap(~ station, nrow=4) +
  theme_cowplot(12) +
  panel_border() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.text=element_text(size=10), axis.title=element_text(size=12), 
        strip.background = element_rect(colour="black", fill="white",linetype="solid"),
        strip.text.x = element_text(size=12, color="black", face="bold"))
ST_2016_b

正如您在上面指出的,您可以使用 year 变量根据自己的喜好设置颜色。