1
votes

I am trying to create a graph with 3 survey seasons, each crossing over a year boundary (ie 2009-2010, 2010-2011, 2016-2017), but where survey effort starts and ends at a different point in each survey season. What I currently have is: enter image description here

I'd like to end up with each x-axis showing November-April of that survey season (ie the 2009-2010 survey season would show November 2009-April 2010) to better show where survey effort is lacking in terms of interannual comparison. I'm just not sure how to do that within the context of scale_x_date(). My current scale statement is simply scale_x_date(date_breaks = '1 month',date_labels='%b')+ My only thought so far is to basically just create a bunch of NA data with the relevant dates and continue to let the dates scale automatically, but for my future reference I'm wondering if there's a better way to create those kind of "year-less" date limits.

Some more information:

the head of my data (which in this case will be in the top-left facet of the plot):

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")))
2
Can you add a (small) example of your dataset to your question?aosmith
Have you tried scales = "free" in facet_grid?Edgar Santos
ed_sans: yes, I have. The result is the figure linked, with limits determined based on data.dtsavage
aosmith: data is up there now.dtsavage

2 Answers

1
votes

We could use the day after Nov-01 to plot in the x-axis, which depends just in the month and day.

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 ~ .)  

enter image description here

PS: I think that adding some NA values (at each Nov-01 and Apr 01) would be more effective than this solution.

Data:

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

Figured this one out, based on some of the stuff that ed_sans suggested. Ended up creating a new column:

Rate_by_site_night$DaysSinceNov1<-as.numeric(Rate_by_site_night$Date-
as.Date(paste0(Rate_by_site_night$season, '-11-01')))

and using that as the x-axis variable with a scale_x_continuous argument using these variables as breaks and labels:

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

Luckily none of my years was a leap year, in which case my month-boundary breaks for March and April would be off.