I am trying to zoom into a specific date range but the code below is not allowing me to do so. Also in this plot are stat_summary by Bleach
and an existing facet by Species
. I am willing to zoom into each species as separate plots if that is a workaround but I've tried subsetting by species and am still unable to produce the desired result.
structure(list(ColonyID = c("11", "11", "11", "11", "11", "11",
"11", "12", "12", "12", "12", "12", "12", "12", "238", "238",
"238", "238", "238", "238", "238", "239", "239", "239", "239",
"239", "239", "239"), Species = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Montipora capitata",
"Porites compressa"), class = "factor"), Bleach = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Bleach",
"Non-bleach"), class = "factor"), Date = structure(c(18096, 18155,
18171, 18185, 18199, 18285, 18501, 18096, 18155, 18171, 18185,
18199, 18285, 18501, 18096, 18155, 18171, 18185, 18199, 18285,
18501, 18096, 18155, 18171, 18185, 18199, 18285, 18501), class =
"Date"),
ColorScore2 = c(3L, 4L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 1L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L,
2L, 1L)), row.names = c(3L, 43L, 83L, 123L, 163L, 203L, 243L,
4L, 44L, 84L, 124L, 164L, 204L, 244L, 34L, 74L, 114L, 154L, 194L,
234L, 274L, 35L, 75L, 115L, 155L, 195L, 235L, 275L), class =
"data.frame")
a <- ggplot(test, aes(x = Date, y = ColorScore2, color = Bleach)) + stat_summary(aes(group =
Bleach), fun = mean, geom = "point") + stat_summary(aes(group = Bleach), fun = mean, geom =
"line") + scale_color_manual(values = c("Bleach" = "gray", "Non-bleach" = "black")) +
scale_fill_manual(values = c("Bleach" = "gray", "Non-bleach" = "black")) +.
scale_x_date(date_breaks = "1 month", date_labels = "%b", limits = as.Date(c("2019-07-01",
"2020-02-01"))) + facet_grid(. ~ Species) + labs(y = "Bleaching Score", x = "Date") +.
theme_bw()
b <- a + facet_zoom(x = Date >= "2019-09-16" & Date <= "2019-10-30")
You code may have some issues:
limits = as.Date(c("2019-07-01", "2020-02-01"))
and usefilter(Date >= as.Date("2019-07-01"), Date <= as.Date("2020-02-01")) %>%
beforeggplot
.facet_grid(. ~ Species)
as it may not work withfacet_zoom
. Instead, I generated two separate graphs, and combined them usingggarrange
fromggpubr
. The code can be simplified but ideas are the same. Hope this helps.