双 y 轴(1 个数据集比另一个大) - 但两个 y 轴仅针对较小的数据集绘制图表

问题描述 投票:0回答:1

我有一个随时间变化的生存情况图,我想将每月降水量放在这些图上。我遇到的问题是我的生存数据集有 8 个点,我的降水数据集有 19 个点,但只有 8 个点的降水数据(与生存数据集相对应的点)正在绘制图表。

我的 prcp 数据应如下所示:

图代码:

pd<-position_dodge(0.2)

merged_data_date %>% 
    ggplot(aes( x=date, y=mean,  ymin=min, ymax=max, color = plot))+
  geom_line(aes(x=date, y=monthprcp*80, color = "blue"))+
  geom_line(show.legend = TRUE,
              size=.75,
              position =pd)+
    # geom_line(y=monthprcp, color ="lightblue")+
    # geom_line(data=mean[!is.na(mean$combo),])+
    geom_hline(yintercept=0, color="#333333", size =0.005)+
    geom_point(size =2, position =pd)+
    geom_errorbar(width = 0.06, position =pd)+
    facet_grid(site~oak,
               switch ="y",
               scales="free",
               labeller  = as_labeller(c("Cortana" = "CT", "JJT" = "JT", 
                                         "Live" = "Live", "Red" = "Red", "Shin"= "Shin")))+
  scale_x_date(date_breaks="4 month", date_labels = "%b %Y")+
# scale_y_continuous (limits =c(0, 24), breaks = seq(0,24, by = 4))+
  scale_y_continuous(name = "Mean survival", sec.axis =sec_axis (trans=~./80, name = "Precipitation"))+
    scale_color_manual(name =NULL,
                       breaks =c("C", "CS", "O", "W", "ligthblue"),
                       values=c( "#C16622FF", "#225EA8","#FECC5C", "#006D2C", "lightblue"),
                       labels =c ("Cage + No shade", "Cage + Shade", "No cage + No shade", "Wooded", "Precipitation"))+
  labs (x="Time",
        y="Mean seedling survival per subplot",
        # title="Seedling survival over time",
        title=NULL,
        # subtitle = "Seedling survival on the Cortana and JJT site. Seedlings were counted between fall
        # 2019 to fall 2020.",
        size = 16)+ 
  # facet_wrap(site~oak, ncol=3, drop = T)+
  theme_minimal()+
  theme (
    legend.justification = "center",
    legend.position = "top",
    strip.placement = "outside",
    # axis.text.x=element_text(angle=45, vjust=0.5),
    plot.title = element_text (size = 16),
    plot.title.position = "plot",
    axis.line.x = element_line(),
    axis.line.y = element_line(),
    axis.title.y = element_text(size =10.5, vjust = 1),
    axis.title.x = element_text(size =10.5, vjust = 1),
    #strip.background = element_blank(),
    strip.text.y = element_text(size =9.5),
    strip.text.x= element_text(size =9.5, margin = margin (0,0,5,0)),
    panel.spacing.x = unit(0.5,"lines"),
    # panel.grid=element_blank())
    #modifies just the vertical graphs (space between top and bottom)
    panel.margin.y=unit (0.4, "lines"),
    panel.grid.minor.y = element_line(color = "lightgrey"))

我的数据集示例

 merged_data_date<-structure(
list(
  oak = c(
    "Live",
    "Red",
    "Shin",
    "Red",
    "Live",
    "Shin",
    "Red",
    "Shin",
    "Red",
    "Shin",
    "Shin",
    "Shin",
    "Shin",
    "Live",
    NA,
    "Shin",
    "Red",
    "Red",
    "Red",
    "Red",
    "Red",
    "Red",
    "Live",
    "Red",
    "Red",
    "Shin",
    "Live",
    "Red",
    "Live",
    "Live",
    "Live",
    "Live",
    "Shin",
    "Red",
    "Red",
    "Red",
    "Shin",
    "Live",
    "Shin",
    "Live",
    "Live",
    "Shin",
    NA,
    NA,
    "Shin",
    "Live",
    "Red",
    "Red",
    "Red",
    NA
  ),
  plot = c(
    "CS",
    "C",
    "C",
    "O",
    "O",
    "C",
    "O",
    "W",
    "O",
    "C",
    "CS",
    "C",
    "CS",
    "W",
    NA,
    "O",
    "W",
    "CS",
    "O",
    "CS",
    "CS",
    "O",
    "C",
    "W",
    "O",
    "O",
    "W",
    "C",
    "O",
    "C",
    "C",
    "O",
    "W",
    "O",
    "O",
    "CS",
    "O",
    "C",
    "CS",
    "CS",
    "O",
    "CS",
    NA,
    NA,
    "C",
    "O",
    "W",
    "W",
    "C",
    NA
  ),
  date = structure(
    c(
      18475,
      18048,
      18109,
      18506,
      18048,
      18078,
      18170,
      18109,
      18048,
      18170,
      18475,
      18506,
      18048,
      18109,
      18017,
      18506,
      18078,
      18048,
      18475,
      17956,
      18506,
      18109,
      18170,
      18078,
      18506,
      18506,
      18078,
      18475,
      18170,
      18475,
      18048,
      18506,
      18078,
      17956,
      18170,
      18475,
      18078,
      18078,
      18078,
      18048,
      18048,
      18109,
      18322,
      18353,
      17956,
      17956,
      18475,
      17956,
      18048,
      18414
    ),
    class = "Date"
  ),
  site = c(
    "Cortana",
    "JJT",
    "Cortana",
    "JJT",
    "JJT",
    "JJT",
    "JJT",
    "JJT",
    "Cortana",
    "Cortana",
    "Cortana",
    "Cortana",
    "JJT",
    "Cortana",
    NA,
    "Cortana",
    "JJT",
    "JJT",
    "Cortana",
    "JJT",
    "JJT",
    "JJT",
    "JJT",
    "Cortana",
    "Cortana",
    "JJT",
    "Cortana",
    "JJT",
    "JJT",
    "Cortana",
    "Cortana",
    "JJT",
    "JJT",
    "Cortana",
    "Cortana",
    "Cortana",
    "JJT",
    "JJT",
    "JJT",
    "JJT",
    "Cortana",
    "JJT",
    NA,
    NA,
    "JJT",
    "JJT",
    "JJT",
    "Cortana",
    "Cortana",
    NA
  ),
  mean = c(
    8.6,
    17.2,
    10.8,
    0,
    8,
    14.2,
    0,
    6,
    15.2,
    10.4,
    3.6,
    2.8,
    18.8,
    4.2,
    NA,
    3.6,
    16.8,
    22.4,
    0,
    14.2,
    18.6,
    17.4,
    0,
    9.6,
    0,
    0,
    4.8,
    0,
    0,
    0.4,
    6.8,
    0,
    6.2,
    7.8,
    15.6,
    7,
    15.6,
    10.6,
    19,
    9.8,
    7,
    19.2,
    NA,
    NA,
    2.4,
    1,
    5.6,
    5.6,
    16.4,
    NA
  ),
  sd = c(
    1.69115345252878,
    1.59373774505092,
    1.82756668824971,
    0,
    0.447213595499958,
    1.2,
    0,
    3.09838667696593,
    1.46287388383278,
    2.22710574513201,
    0.92736184954957,
    1.35646599662505,
    1.39283882771841,
    1.24096736459909,
    NA,
    1.6,
    1.98494332412792,
    1.20830459735946,
    0,
    2.24499443206436,
    2.11187120819429,
    0.6,
    0,
    3.85486705347928,
    0,
    0,
    1.31909059582729,
    0,
    0,
    0.4,
    0.374165738677394,
    0,
    3.18433666561813,
    1.15758369027902,
    1.4,
    1.92353840616713,
    0.509901951359279,
    0.748331477354788,
    1.09544511501033,
    1.49666295470958,
    1.30384048104053,
    1.01980390271856,
    NA,
    NA,
    0.678232998312527,
    0.632455532033676,
    2.92574776766556,
    1.69115345252878,
    1.80554700852678,
    NA
  ),
  max = c(
    10.2911534525288,
    18.7937377450509,
    12.6275666882497,
    0,
    8.44721359549996,
    15.4,
    0,
    9.09838667696593,
    16.6628738838328,
    12.627105745132,
    4.52736184954957,
    4.15646599662505,
    20.1928388277184,
    5.44096736459909,
    NA,
    5.2,
    18.7849433241279,
    23.6083045973595,
    0,
    16.4449944320644,
    20.7118712081943,
    18,
    0,
    13.4548670534793,
    0,
    0,
    6.11909059582729,
    0,
    0,
    0.8,
    7.17416573867739,
    0,
    9.38433666561813,
    8.95758369027902,
    17,
    8.92353840616713,
    16.1099019513593,
    11.3483314773548,
    20.0954451150103,
    11.2966629547096,
    8.30384048104053,
    20.2198039027186,
    NA,
    NA,
    3.07823299831253,
    1.63245553203368,
    8.52574776766556,
    7.29115345252878,
    18.2055470085268,
    NA
  ),
  min = c(
    6.90884654747122,
    15.6062622549491,
    8.97243331175029,
    0,
    7.55278640450004,
    13,
    0,
    2.90161332303407,
    13.7371261161672,
    8.17289425486799,
    2.67263815045043,
    1.44353400337495,
    17.4071611722816,
    2.95903263540091,
    NA,
    2,
    14.8150566758721,
    21.1916954026405,
    0,
    11.9550055679356,
    16.4881287918057,
    16.8,
    0,
    5.74513294652072,
    0,
    0,
    3.48090940417271,
    0,
    0,
    5.55111512312578e-17,
    6.42583426132261,
    0,
    3.01566333438187,
    6.64241630972098,
    14.2,
    5.07646159383287,
    15.0900980486407,
    9.85166852264521,
    17.9045548849897,
    8.30333704529042,
    5.69615951895947,
    18.1801960972814,
    NA,
    NA,
    1.72176700168747,
    0.367544467966324,
    2.67425223233444,
    3.90884654747122,
    14.5944529914732,
    NA
  ),
  monthprcp = c(
    0.0496551724137931,
    0.1475,
    0.0316071428571429,
    0.221166666666667,
    0.1475,
    0.0404838709677419,
    0.140645161290323,
    0.0316071428571429,
    0.1475,
    0.140645161290323,
    0.0496551724137931,
    0.221166666666667,
    0.1475,
    0.0316071428571429,
    0.207692307692308,
    0.221166666666667,
    0.0404838709677419,
    0.1475,
    0.0496551724137931,
    0.0323214285714286,
    0.221166666666667,
    0.0316071428571429,
    0.140645161290323,
    0.0404838709677419,
    0.221166666666667,
    0.221166666666667,
    0.0404838709677419,
    0.0496551724137931,
    0.140645161290323,
    0.0496551724137931,
    0.1475,
    0.221166666666667,
    0.0404838709677419,
    0.0323214285714286,
    0.140645161290323,
    0.0496551724137931,
    0.0404838709677419,
    0.0404838709677419,
    0.0404838709677419,
    0.1475,
    0.1475,
    0.0316071428571429,
    0.0920967741935484,
    0.0646551724137931,
    0.0323214285714286,
    0.0323214285714286,
    0.0496551724137931,
    0.0323214285714286,
    0.1475,
    0.069
  )
),
row.names = c(NA,-50L),
class = c("tbl_df",
          "tbl", "data.frame")
)

我尝试过滤 NA,这给了我一个没有 NA 行或 NA 列的图表,但也删除了我的完整日期数据集

merged_data<-full_join(final_survprcp1, finalprcp, by = "date") %>% 
  mutate(date=as.yearmon(date)) %>% 
  mutate(date=as.Date(date)) %>% 
  filter(!is.na(site)) %>% 
  filter(!is.na(plot)) %>% 
  filter(!is.na(oak)) %>% 
  mutate(monthprcp_tr=monthprcp*80)

我还希望我的网站名称被剥离放置在外面,不知道为什么该代码不起作用。 我还尝试将图中的 prcp 线更改为蓝色,但线条颜色没有改变。

希望这是有道理的。

r ggplot2 facet-wrap
1个回答
0
投票

如果我理解你的问题,我认为让两个不同的数据集保持不同可能会更容易。如果这样做,那么您可以独立于生存数据点绘制降水数据线。

输出

首先,我将把两个数据集分开。

library(dplyr)
library(ggplot2)
library(tidyr)


precip_data <- merged_data_date %>% 
  group_by(date) %>% 
  summarise(monthprcp = first(monthprcp))

surv_data <- merged_data_date %>% 
  dplyr::select(-monthprcp) %>% 
  na.omit()

为了使其按您的预期工作,您需要复制所有级别

site
oak
的降水数据,您可以执行以下操作:

precip_data <- expand_grid(oak = c("Live", "Red", "Shin"), 
                           site = c("Cortana", "JJT"), 
                           date = precip_data$date) %>% 
  left_join(precip_data)

然后你就可以制作情节了。请注意,在使用降水数据进行绘图时,我使用

inherit.aes=FALSE
,这将允许渲染
precip_data
中的属性,而不管它们是否与调用
ggplot()
中的美学元素和数据相对应。

pd<-position_dodge(0.2)

ggplot(data = surv_data, 
       aes( x=date, y=mean,  ymin=min, ymax=max, color = plot)) + 
  geom_line(data = precip_data, aes(x=date, y=monthprcp*80), color="black", inherit.aes = FALSE) + 
  geom_line(show.legend = TRUE,
            linewidth=.75,
            position =pd)+
  geom_hline(yintercept=0, color="#333333", linewidth =0.005)+
  geom_point(size =2, position =pd)+
  facet_grid(site~oak,
             switch ="y",
             scales="free",
             labeller  = as_labeller(c("Cortana" = "CT", "JJT" = "JT", 
                                       "Live" = "Live", "Red" = "Red", "Shin"= "Shin"))) + 
  scale_x_date(date_breaks="4 month", date_labels = "%b %Y")+
  # scale_y_continuous (limits =c(0, 24), breaks = seq(0,24, by = 4))+
  scale_y_continuous(name = "Mean survival", sec.axis =sec_axis (trans=~./80, name = "Precipitation"))+
  scale_color_manual(name =NULL,
                     breaks =c("C", "CS", "O", "W", "ligthblue"),
                     values=c( "#C16622FF", "#225EA8","#FECC5C", "#006D2C", "lightblue"),
                     labels =c ("Cage + No shade", "Cage + Shade", "No cage + No shade", "Wooded", "Precipitation"))+
  labs (x="Time",
        y="Mean seedling survival per subplot",
        # title="Seedling survival over time",
        title=NULL,
        # subtitle = "Seedling survival on the Cortana and JJT site. Seedlings were counted between fall
        # 2019 to fall 2020.",
        size = 16)+ 
  # facet_wrap(site~oak, ncol=3, drop = T)+
  theme_minimal()+
  theme (
    legend.justification = "center",
    legend.position = "top",
    strip.placement = "outside",
    # axis.text.x=element_text(angle=45, vjust=0.5),
    plot.title = element_text (size = 16),
    plot.title.position = "plot",
    axis.line.x = element_line(),
    axis.line.y = element_line(),
    axis.title.y = element_text(size =10.5, vjust = 1),
    axis.title.x = element_text(size =10.5, vjust = 1),
    #strip.background = element_blank(),
    strip.text.y = element_text(size =9.5),
    strip.text.x= element_text(size =9.5, margin = margin (0,0,5,0)),
    panel.spacing.x = unit(0.5,"lines"),
    # panel.grid=element_blank())
    #modifies just the vertical graphs (space between top and bottom)
    panel.margin.y=unit (0.4, "lines"),
    panel.grid.minor.y = element_line(color = "lightgrey"))

创建于 2024-01-16,使用 reprex v2.0.2

输入

merged_data_date<-structure(list(oak = c("Live", "Red", "Shin", "Red", "Live", 
"Shin", "Red", "Shin", "Red", "Shin", "Shin", "Shin", "Shin", 
"Live", NA, "Shin", "Red", "Red", "Red", "Red", "Red", "Red", 
"Live", "Red", "Red", "Shin", "Live", "Red", "Live", "Live", 
"Live", "Live", "Shin", "Red", "Red", "Red", "Shin", "Live", 
"Shin", "Live", "Live", "Shin", NA, NA, "Shin", "Live", "Red", 
"Red", "Red", NA), plot = c("CS", "C", "C", "O", "O", "C", "O", 
"W", "O", "C", "CS", "C", "CS", "W", NA, "O", "W", "CS", "O", 
"CS", "CS", "O", "C", "W", "O", "O", "W", "C", "O", "C", "C", 
"O", "W", "O", "O", "CS", "O", "C", "CS", "CS", "O", "CS", NA, 
NA, "C", "O", "W", "W", "C", NA), date = structure(c(18475, 18048, 
18109, 18506, 18048, 18078, 18170, 18109, 18048, 18170, 18475, 
18506, 18048, 18109, 18017, 18506, 18078, 18048, 18475, 17956, 
18506, 18109, 18170, 18078, 18506, 18506, 18078, 18475, 18170, 
18475, 18048, 18506, 18078, 17956, 18170, 18475, 18078, 18078, 
18078, 18048, 18048, 18109, 18322, 18353, 17956, 17956, 18475, 
17956, 18048, 18414), class = "Date"), site = c("Cortana", "JJT", 
"Cortana", "JJT", "JJT", "JJT", "JJT", "JJT", "Cortana", "Cortana", 
"Cortana", "Cortana", "JJT", "Cortana", NA, "Cortana", "JJT", 
"JJT", "Cortana", "JJT", "JJT", "JJT", "JJT", "Cortana", "Cortana", 
"JJT", "Cortana", "JJT", "JJT", "Cortana", "Cortana", "JJT", 
"JJT", "Cortana", "Cortana", "Cortana", "JJT", "JJT", "JJT", 
"JJT", "Cortana", "JJT", NA, NA, "JJT", "JJT", "JJT", "Cortana", 
"Cortana", NA), mean = c(8.6, 17.2, 10.8, 0, 8, 14.2, 0, 6, 15.2, 
10.4, 3.6, 2.8, 18.8, 4.2, NA, 3.6, 16.8, 22.4, 0, 14.2, 18.6, 
17.4, 0, 9.6, 0, 0, 4.8, 0, 0, 0.4, 6.8, 0, 6.2, 7.8, 15.6, 7, 
15.6, 10.6, 19, 9.8, 7, 19.2, NA, NA, 2.4, 1, 5.6, 5.6, 16.4, 
NA), sd = c(1.69115345252878, 1.59373774505092, 1.82756668824971, 
0, 0.447213595499958, 1.2, 0, 3.09838667696593, 1.46287388383278, 
2.22710574513201, 0.92736184954957, 1.35646599662505, 1.39283882771841, 
1.24096736459909, NA, 1.6, 1.98494332412792, 1.20830459735946, 
0, 2.24499443206436, 2.11187120819429, 0.6, 0, 3.85486705347928, 
0, 0, 1.31909059582729, 0, 0, 0.4, 0.374165738677394, 0, 3.18433666561813, 
1.15758369027902, 1.4, 1.92353840616713, 0.509901951359279, 0.748331477354788, 
1.09544511501033, 1.49666295470958, 1.30384048104053, 1.01980390271856, 
NA, NA, 0.678232998312527, 0.632455532033676, 2.92574776766556, 
1.69115345252878, 1.80554700852678, NA), max = c(10.2911534525288, 
18.7937377450509, 12.6275666882497, 0, 8.44721359549996, 15.4, 
0, 9.09838667696593, 16.6628738838328, 12.627105745132, 4.52736184954957, 
4.15646599662505, 20.1928388277184, 5.44096736459909, NA, 5.2, 
18.7849433241279, 23.6083045973595, 0, 16.4449944320644, 20.7118712081943, 
18, 0, 13.4548670534793, 0, 0, 6.11909059582729, 0, 0, 0.8, 7.17416573867739, 
0, 9.38433666561813, 8.95758369027902, 17, 8.92353840616713, 
16.1099019513593, 11.3483314773548, 20.0954451150103, 11.2966629547096, 
8.30384048104053, 20.2198039027186, NA, NA, 3.07823299831253, 
1.63245553203368, 8.52574776766556, 7.29115345252878, 18.2055470085268, 
NA), min = c(6.90884654747122, 15.6062622549491, 8.97243331175029, 
0, 7.55278640450004, 13, 0, 2.90161332303407, 13.7371261161672, 
8.17289425486799, 2.67263815045043, 1.44353400337495, 17.4071611722816, 
2.95903263540091, NA, 2, 14.8150566758721, 21.1916954026405, 
0, 11.9550055679356, 16.4881287918057, 16.8, 0, 5.74513294652072, 
0, 0, 3.48090940417271, 0, 0, 5.55111512312578e-17, 6.42583426132261, 
0, 3.01566333438187, 6.64241630972098, 14.2, 5.07646159383287, 
15.0900980486407, 9.85166852264521, 17.9045548849897, 8.30333704529042, 
5.69615951895947, 18.1801960972814, NA, NA, 1.72176700168747, 
0.367544467966324, 2.67425223233444, 3.90884654747122, 14.5944529914732, 
NA), monthprcp = c(0.0496551724137931, 0.1475, 0.0316071428571429, 
0.221166666666667, 0.1475, 0.0404838709677419, 0.140645161290323, 
0.0316071428571429, 0.1475, 0.140645161290323, 0.0496551724137931, 
0.221166666666667, 0.1475, 0.0316071428571429, 0.207692307692308, 
0.221166666666667, 0.0404838709677419, 0.1475, 0.0496551724137931, 
0.0323214285714286, 0.221166666666667, 0.0316071428571429, 0.140645161290323, 
0.0404838709677419, 0.221166666666667, 0.221166666666667, 0.0404838709677419, 
0.0496551724137931, 0.140645161290323, 0.0496551724137931, 0.1475, 
0.221166666666667, 0.0404838709677419, 0.0323214285714286, 0.140645161290323, 
0.0496551724137931, 0.0404838709677419, 0.0404838709677419, 0.0404838709677419, 
0.1475, 0.1475, 0.0316071428571429, 0.0920967741935484, 0.0646551724137931, 
0.0323214285714286, 0.0323214285714286, 0.0496551724137931, 0.0323214285714286, 
0.1475, 0.069)), row.names = c(NA, -50L), class = c("tbl_df", 
"tbl", "data.frame"))
© www.soinside.com 2019 - 2024. All rights reserved.