简体   繁体   English

ggplot“日-月”x 轴,分面包裹“年”

[英]ggplot “day-month” x axis with facet wrap “year”

I have a problem that I cannot resolve with ggplot.我有一个无法用 ggplot 解决的问题。 I have "sum" data based on campaign dates and seasons.我有基于活动日期和季节的“总和”数据。 A season begins in September of the year and ends in February of year n + 1. I would like to create a ggplot with facet wrap of the season, but with the "days-month" in x axis so that each point has its date (30/11 for example).一个季节从一年中的 9 月开始,在 n + 1 年的 2 月结束。我想创建一个带有季节分面包装的 ggplot,但在 x 轴上使用“天-月”,以便每个点都有它的日期(例如 30/11)。

This is a manipulation that I managed to perform with plot() but season per season :这是我设法使用 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")

However, is it possible to do this manipulation under ggplot so that each facet is centered on the day / month dates of the season in question starting with the month of September and ending in the month of February?但是,是否可以在 ggplot 下进行这种操作,以便每个方面都以所讨论季节的日期/月份日期为中心,从 9 月份开始到 2 月份结束?

in the idea of在想法中

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

here is my dataset这是我的数据集

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

Thanks !谢谢 !

I think you're looking for scales = "free_x" in facet_wrap :我认为你正在寻找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))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM