• # Set same date range and interval on facets

David updated 6 months, 1 week ago 2 Members · 4 Posts
• ### yuri.zharikov

Member
March 5, 2021 at 12:42 pm

Hi Dave,

Here is my sample data set and code:

range1 <- seq.Date(as.Date(“2018-10-08”), as.Date(“2018-12-05”), by=7)

range2 <- seq.Date(as.Date(“2020-10-01”), as.Date(“2020-12-15”), by=7)

Dates <-as.POSIXct(c(range1, range2))

Count <- sample.int(500, 19, replace = F)

Sp <- c(“A”, “B”)

test.d <- as.data.frame(cbind(Sp, Count, format(Dates, “%Y-%m-%d”)))

test.d <- test.d %>% rename(Date = V3) %>% mutate(Season = lubridate::year(Date),

Count = as.numeric(Count))

glimpse(test.d)

ggplot(test.d, aes(as.Date(Date), Count))+

geom_col()+

facet_grid(Sp~Season, scales=”free”)+labs(x = “”, y = “Daily Count”)+

scale_x_date(date_breaks = “5 days”, date_labels = “%d/%m”)

#what I am trying to achieve is to have the same date range and interval on the x-axis

Thank you!

Yuri

• ### yuri.zharikov

Member
March 5, 2021 at 2:25 pm

Hi Dave,

I solved the problem by converting the date to julian date and setting limits and breaks on julian date (integer), then formatting it to display as day/month. So all good here. Thank you.

Yuri

• ### yuri.zharikov

Member
March 5, 2021 at 2:27 pm

+scale_x_continuous(limits = c(280, 345), breaks = seq(280, 345, 14), labels = function(x) format(as.Date(as.character(x), “%j”), “%d-%b”))

where x is a jul.date defined in the ggplot aes() call

• ### David

Organizer
March 7, 2021 at 8:33 pm

Just had a chance to review this. Glad you got it figured out!

Viewing 1 - 4 of 4 replies