我正在尝试创建一个包含3个调查季节的图表,每个季度跨越一年的边界(即2009-2010,2010-2011,2016-2017),但调查工作的开始和结束在每个调查的不同点季节。我现在拥有的是:
我希望每个x轴显示该调查季节的11月至4月(即2009-2010调查季将显示2009年11月至2010年4月),以更好地显示调查工作缺乏的地方年际比较。我只是不确定如何在scale_x_date()
的背景下做到这一点。我当前的缩放语句只是scale_x_date(date_breaks = '1 month',date_labels='%b')+
我到目前为止唯一的想法是基本上只是创建一堆具有相关日期的NA数据并继续让日期自动缩放,但是为了我将来的参考,我想知道是否有一种更好的方法来创造那种“无年”的约会限制。
更多信息:
我的数据的头部(在这种情况下将位于情节的左上方):
structure(list(SPID = c("Cho_001", "Cho_001", "Cho_001", "Cho_001",
"Cho_001", "Cho_001"), season = c(2009, 2009, 2009, 2009, 2009,
2009), Date = structure(c(14579, 14580, 14581, 14582, 14583,
14584), class = "Date"), DayOfYear = c(335, 336, 337, 338, 339,
340), year = c(2009L, 2009L, 2009L, 2009L, 2009L, 2009L), month = c(12L,
12L, 12L, 12L, 12L, 12L), day = 1:6, RatePerMin = c(3.6667, 4.8667,
13.1667, 23.0333, 24.6667, 25.4667), N = c(3L, 30L, 30L, 30L,
30L, 30L), sd = c(3.5119, 6.9269, 8.1074, 4.2789, 4.9434, 4.2729
), se = c(2.0276, 1.2647, 1.4802, 0.7812, 0.9025, 0.7801), Illu = c(0.999348417128253,
0.999986511310919, 0.991435775301254, 0.955915838251852, 0.894428866388813,
0.810868946410883)), .Names = c("SPID", "season", "Date", "DayOfYear",
"year", "month", "day", "RatePerMin", "N", "sd", "se", "Illu"
), row.names = c(NA, -6L), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), vars = c("SPID", "season", "year", "month",
"day", "Date"), drop = TRUE, indices = list(0L, 1L, 2L, 3L, 4L,
5L), group_sizes = c(1L, 1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
SPID = c("Cho_001", "Cho_001", "Cho_001", "Cho_001", "Cho_001",
"Cho_001"), season = c(2009, 2009, 2009, 2009, 2009, 2009
), year = c(2009L, 2009L, 2009L, 2009L, 2009L, 2009L), month = c(12L,
12L, 12L, 12L, 12L, 12L), day = 1:6, Date = structure(c(14579,
14580, 14581, 14582, 14583, 14584), class = "Date")), row.names = c(NA,
-6L), class = "data.frame", vars = c("SPID", "season", "year",
"month", "day", "Date"), drop = TRUE, .Names = c("SPID", "season",
"year", "month", "day", "Date")))
答案 0 :(得分:1)
我们可以使用11月1日之后的日子在x轴上绘图,这取决于月和日。
data$start <- ymd(paste(year(data$Date), "-11-01", sep=""))
data$yday = data$Date - data$start #yday(data$Date)
data$yday = ifelse(data$yday < 0, 365 + data$yday,data$yday)
bre <- seq(1,152,1)
lab <- format(seq.Date(ymd("2010-11-01"),ymd("2011-04-01"),by="day"),format= "%b %d")
library(ggplot2)
library(lubridate)
x11(); ggplot(data) + geom_bar(aes(x = yday , y = RatePerMin), stat = "identity") +
scale_x_continuous(breaks = bre,
labels = lab, limits = c(1,152))+ theme(axis.text.x = element_text(angle = 90, hjust = 1))+
facet_grid(year ~ .)
PS:我认为添加一些NA值(在每个11月1日和4月01日)将比这个解决方案更有效。
数据:
structure(list(SPID = c("Cho_001", "Cho_001", "Cho_001", "Cho_001",
"Cho_001", "Cho_001", "Cho_001", "Cho_001", "Cho_001", "Cho_001",
"Cho_001", "Cho_001", "Cho_001", "Cho_001", "Cho_001"), season = c(2009,
2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009,
2009, 2009, 2009), Date = structure(c(14579, 14580, 14581, 14582,
14583, 14584, 14944, 14945, 14946, 14947, 14948, 14949, 14610,
14611, 14612), class = "Date"), DayOfYear = c(335, 336, 337,
338, 339, 340, 335, 336, 337, 338, 339, 340, 335, 336, 337),
year = c(2009, 2009, 2009, 2009, 2009, 2009, 2010, 2010,
2010, 2010, 2010, 2010, 2010, 2010, 2010), month = c(12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L), day = c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L,
5L, 6L, 1L, 2L, 3L), RatePerMin = c(3.6667, 4.8667, 13.1667,
23.0333, 24.6667, 25.4667, 3.6667, 4.8667, 13.1667, 23.0333,
24.6667, 25.4667, 3.6667, 4.8667, 13.1667), N = c(3L, 30L,
30L, 30L, 30L, 30L, 3L, 30L, 30L, 30L, 30L, 30L, 3L, 30L,
30L), sd = c(3.5119, 6.9269, 8.1074, 4.2789, 4.9434, 4.2729,
3.5119, 6.9269, 8.1074, 4.2789, 4.9434, 4.2729, 3.5119, 6.9269,
8.1074), se = c(2.0276, 1.2647, 1.4802, 0.7812, 0.9025, 0.7801,
2.0276, 1.2647, 1.4802, 0.7812, 0.9025, 0.7801, 2.0276, 1.2647,
1.4802), Illu = c(0.999348417128253, 0.999986511310919, 0.991435775301254,
0.955915838251852, 0.894428866388813, 0.810868946410883,
0.999348417128253, 0.999986511310919, 0.991435775301254,
0.955915838251852, 0.894428866388813, 0.810868946410883,
0.999348417128253, 0.999986511310919, 0.991435775301254),
yday = c(30, 31, 32, 33, 34, 35, 30, 31, 32, 33, 34, 35,
61, 62, 63), start = structure(c(14549, 14549, 14549, 14549,
14549, 14549, 14914, 14914, 14914, 14914, 14914, 14914, 14914,
14914, 14914), class = "Date"), end = structure(c(14700,
14700, 14700, 14700, 14700, 14700, 15065, 15065, 15065, 15065,
15065, 15065, 15065, 15065, 15065), class = "Date")), .Names = c("SPID",
"season", "Date", "DayOfYear", "year", "month", "day", "RatePerMin",
"N", "sd", "se", "Illu", "yday", "start", "end"), row.names = c(NA,
15L), vars = c("SPID", "season", "year", "month", "day", "Date"
), drop = TRUE, indices = list(0L, 1L, 2L, 3L, 4L, 5L), group_sizes = c(1L,
1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
SPID = c("Cho_001", "Cho_001", "Cho_001", "Cho_001", "Cho_001",
"Cho_001"), season = c(2009, 2009, 2009, 2009, 2009, 2009
), year = c(2009L, 2009L, 2009L, 2009L, 2009L, 2009L), month = c(12L,
12L, 12L, 12L, 12L, 12L), day = 1:6, Date = structure(c(14579,
14580, 14581, 14582, 14583, 14584), class = "Date")), .Names = c("SPID",
"season", "year", "month", "day", "Date"), row.names = c(NA,
-6L), class = "data.frame", vars = c("SPID", "season", "year",
"month", "day", "Date"), drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
答案 1 :(得分:0)
根据ed_sans建议的一些内容计算出这个。结束创建新列:
Rate_by_site_night$DaysSinceNov1<-as.numeric(Rate_by_site_night$Date-
as.Date(paste0(Rate_by_site_night$season, '-11-01')))
并将其用作带有scale_x_continuous
参数的x轴变量,并将这些变量用作断点和标签:
test_breaks=c(0,30,61,92,120,151) #month boundaries in terms of days since Nov 1
test_labels=format(as.Date(test_breaks, origin='2016-11-01'), format= '%b')
幸运的是,我的岁月都不是闰年,在这种情况下,3月和4月的月份界限将会被取消。