1

我正在尝试放大特定的日期范围,但下面的代码不允许我这样做。在这个图中还有 stat_summary byBleach和现有 facet by Species。如果这是一种解决方法,我愿意将每个物种放大为单独的图,但我已经尝试按物种进行子集化,但仍然无法产生预期的结果。

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

1 回答 1

1

您的代码可能有一些问题:

  1. 我之前删除limits = as.Date(c("2019-07-01", "2020-02-01"))并使用filter(Date >= as.Date("2019-07-01"), Date <= as.Date("2020-02-01")) %>%ggplot
  2. 我删除facet_grid(. ~ Species)了,因为它可能不适用于facet_zoom. 相反,我生成了两个单独的图表,并使用ggarrangefrom将它们组合在一起ggpubr。代码可以简化,但思路是一样的。希望这可以帮助。
library(tidyverse)
library(ggpubr)

a_1 <- test %>%
  filter(Date >= as.Date("2019-07-01"), Date <= as.Date("2020-02-01"), 
         Species == "Montipora capitata"
         ) %>%
  ggplot(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") +
  theme_bw() + 
  labs(title = "Montipora capitata")
b_1 <- a_1 + facet_zoom(xlim = c(as.Date("2019-09-16"), as.Date("2019-10-30")))

b_1
a_2 <- test %>%
  filter(Date >= as.Date("2019-07-01"), Date <= as.Date("2020-02-01"), 
         Species == "Porites compressa"
  ) %>%
  ggplot(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") +
  theme_bw()+
  labs(title = "Porites compressa")
b_2 <- a_2 + facet_zoom(xlim = c(as.Date("2019-09-16"), as.Date("2019-10-30")))
ggarrange(b_1, b_2, common.legend = TRUE)

在此处输入图像描述

于 2020-09-30T00:24:39.480 回答