ggplot“ day-month” x轴,带多面环绕式“ year”

时间:2020-09-07 09:52:45

标签: r ggplot2 lubridate

我有一个无法使用ggplot解决的问题。我有基于活动日期和季节的“总和”数据。一个季节从一年的9月开始,到n + 1的2月结束。我想创建一个具有该季节的多面包装的ggplot,但是在x轴上带有“ days-month”,因此每个点都有其日期(例如30/11)。

这是我设法通过plot()执行的操作,但是每个季节都有一个季节:

plot(data$Sum ~ data$Date, type = "o", pch = 20, cex = 0.8, xaxt ="n", main="Season x")
axis.POSIXct(1, at=data$Date, format="%d/%m", las = 2, cex.axis = 0.7, tck = 0.01, col = "black")

但是,是否有可能在ggplot下进行此操作,以使每个方面都集中在相关季节的日期/月份日期(从9月开始到2月结束)?

的想法

  data %>% ggplot(aes(x=Date, y=Sum)) + 
  geom_line() + geom_point() + theme_light() + facet_wrap(.~Season) + scale_x_date(date_labels = "%d-%m")

这是我的数据集

data <- structure(list(Date = structure(c(1257030000, 1257548400, 1257634800, 
1258153200, 1258239600, 1258758000, 1258844400, 1259362800, 1259449200, 
1259967600, 1260054000, 1260572400, 1260658800, 1261177200, 1261263600, 
1262991600, 1263078000, 1263596400, 1263682800, 1264201200, 1264287600, 
1264806000, 1264892400, 1265410800, 1265497200, 1266015600, 1266102000, 
1288998000, 1289084400, 1289602800, 1289689200, 1290207600, 1290294000, 
1290812400, 1290898800, 1291417200, 1291503600, 1292022000, 1292108400, 
1292626800, 1293318000, 1294441200, 1294527600, 1295046000, 1295132400, 
1295650800, 1295737200, 1296255600, 1296342000, 1296946800, 1297033200, 
1297465200, 1297551600, 1298070000, 1298156400, 1298674800, 1298761200
), class = c("POSIXct", "POSIXt"), tzone = ""), Sum = c(0.365853658536585, 
0.395833333333333, 0.631578947368421, 0.727272727272727, 0.375, 
0.578947368421053, 0.807017543859649, 0.732142857142857, 0.472727272727273, 
0.267857142857143, 0.48, 0.4, 0.666666666666667, 0.218181818181818, 
0.322033898305085, 0.272727272727273, 0.42, 0.245283018867925, 
0.230769230769231, 0.607843137254902, 0.588235294117647, 0.384615384615385, 
0.80952380952381, 0.148936170212766, 0.542857142857143, 0.358974358974359, 
0.30952380952381, 0.553571428571429, 0.333333333333333, 0.62, 
0.54, 0.254545454545455, 0.4, 0.145454545454545, 0.685185185185185, 
0.339737108190091, 0.361702127659574, 0.403846153846154, 0.839285714285714, 
0.08, 0.423076923076923, 0.181818181818182, 0.732142857142857, 
0.574074074074074, 0.509090909090909, 0.381818181818182, 0.66, 
0.297872340425532, 0.392156862745098, 0.333333333333333, 0.119047619047619, 
0.333333333333333, 0.348837209302326, 0.352941176470588, 0.358974358974359, 
0.32258064516129, 0.3125), Season = c("Season 2009-2010", "Season 2009-2010", 
"Season 2009-2010", "Season 2009-2010", "Season 2009-2010", "Season 2009-2010", 
"Season 2009-2010", "Season 2009-2010", "Season 2009-2010", "Season 2009-2010", 
"Season 2009-2010", "Season 2009-2010", "Season 2009-2010", "Season 2009-2010", 
"Season 2009-2010", "Season 2009-2010", "Season 2009-2010", "Season 2009-2010", 
"Season 2009-2010", "Season 2009-2010", "Season 2009-2010", "Season 2009-2010", 
"Season 2009-2010", "Season 2009-2010", "Season 2009-2010", "Season 2009-2010", 
"Season 2009-2010", "Season 2010-2011", "Season 2010-2011", "Season 2010-2011", 
"Season 2010-2011", "Season 2010-2011", "Season 2010-2011", "Season 2010-2011", 
"Season 2010-2011", "Season 2010-2011", "Season 2010-2011", "Season 2010-2011", 
"Season 2010-2011", "Season 2010-2011", "Season 2010-2011", "Season 2010-2011", 
"Season 2010-2011", "Season 2010-2011", "Season 2010-2011", "Season 2010-2011", 
"Season 2010-2011", "Season 2010-2011", "Season 2010-2011", "Season 2010-2011", 
"Season 2010-2011", "Season 2010-2011", "Season 2010-2011", "Season 2010-2011", 
"Season 2010-2011", "Season 2010-2011", "Season 2010-2011")), row.names = c(NA, 
-57L), class = "data.frame")

谢谢!

1 个答案:

答案 0 :(得分:1)

我认为您正在scales = "free_x"中寻找facet_wrap

data %>% 
  ggplot(aes(x=Date, y=Sum)) + 
  geom_line() + 
  geom_point() + 
  theme_classic() + 
  geom_rug(sides = "b") +
  facet_wrap(.~Season, scales = "free_x") + 
  scale_x_datetime(date_labels = "%d-%m", breaks = data$Date) +
  guides(x = guide_axis(check.overlap = TRUE, angle = 90)) +
  theme(strip.background = element_rect(fill = "gray90"),
        panel.border = element_rect(colour = "black", fill = NA))

enter image description here