【问题标题】:Automatically make time series plots for each level in a group自动为组中的每个级别制作时间序列图
【发布时间】:2021-11-04 15:10:16
【问题描述】:

我的原始问题可以在这里看到 (Automating a ggplot for each level in a group),但我想我会提出不同的要求,以便让它以多种不同的方式来回答这个问题,而不是一个“如何”的问题来解决我的糟糕尝试.

我想让创建如下图的时间序列图的过程更快/自动(即,不需要用户一次输入一个物种名称)。也许有一个“if”循环。告诉 R 循环遍历数据中所有唯一的通用名称并使用下面的代码打印(或保存到 png)图的东西(每个物种的“common_name”作为它们各自图的标题)。如果没有足够的数据用于绘图,R 应该打印一条消息:“没有足够的数据用于绘图”或其他内容。

这是我的数据样本(如您所见,有超过 100 种物种可供绘制)。此数据样本仅显示 3 个物种,47 个地点中的 5 个,以及 16 年数据中的 3 年。

data <- structure(list(year = c(2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 
2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 
2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 
2020L, 2020L, 2020L), season = structure(c(1L, 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, 2L, 1L, 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, 2L, 1L, 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, 2L), .Label = c("dry", "wet"), class = "factor"), 
    site = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 
    5L, 5L, 5L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 
    5L, 5L, 5L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 
    5L, 5L, 5L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 
    5L, 5L, 5L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 
    5L, 5L, 5L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 
    5L, 5L, 5L), common_name = structure(c(68L, 92L, 105L, 68L, 
    92L, 105L, 68L, 92L, 105L, 68L, 92L, 105L, 68L, 92L, 105L, 
    68L, 92L, 105L, 68L, 92L, 105L, 68L, 92L, 105L, 68L, 92L, 
    105L, 68L, 92L, 105L, 68L, 92L, 105L, 68L, 92L, 105L, 68L, 
    92L, 105L, 68L, 92L, 105L, 68L, 92L, 105L, 68L, 92L, 105L, 
    68L, 92L, 105L, 68L, 92L, 105L, 68L, 92L, 105L, 68L, 92L, 
    105L, 68L, 92L, 105L, 68L, 92L, 105L, 68L, 92L, 105L, 68L, 
    92L, 105L, 68L, 92L, 105L, 68L, 92L, 105L, 68L, 92L, 105L, 
    68L, 92L, 105L, 68L, 92L, 105L, 68L, 92L, 105L), .Label = c("Atlantic Mud Crab", 
    "Atlantic Needlefish", "Banded Blenny", "Banded Brittle Star", 
    "Banded Killifish", "Bandtail Puffer", "Barracuda spp", "Bigclaw Snapping Shrimp", 
    "Bigeye Mojarra", "Blenny spp", "Blue Crab", "Blue Crab spp", 
    "Blue Striped Grunt", "Bluethroat Pikeblenny", "Bonefish", 
    "Brittle Star spp", "Broadback Mud Crab", "Brown Shrimp", 
    "Bryozoan Shrimp", "Chain Pipefish", "Checkered Puffer", 
    "Chub spp", "Clown Goby", "Code Goby", "Combtooth Blenny spp", 
    "Crested Blenny", "Crested Goby", "Crossbanded Grass Shrimp", 
    "Cushion Sea Star", "Daggerblade Grass Shrimp", "Darter Goby", 
    "Dusky Pipefish", "Dwarf Seahorse", "Estuarine Snapping Shrimp", 
    "False Zostera Shrimp", "Fiddler Crab spp", "Flagfin Mojarra", 
    "Flatback Mud Crab", "Florida Blenny", "Florida Grass Shrimp", 
    "Florida Grassflat Crab", "Frillfin Goby", "Fringed Pipefish", 
    "Furrowed Mud Crab", "Giant Decorator crab", "Giant Tiger Prawn", 
    "Glass Shrimp", "Goby spp", "Goby spp (Ctenogobius spp)", 
    "Goldspotted Killifish", "Grass Shrimp (H obliquimanus)", 
    "Grass Shrimp (Leander spp)", "Grass Shrimp (Nikoides schmitti)", 
    "Grass Shrimp (P mundusnovus)", "Grass Shrimp (Palaemon spp)", 
    "Grass Shrimp (Palaemonidae spp)", "Grass Shrimp (Periclimenes spp)", 
    "Grass Shrimp (Thor spp)", "Grass Shrimp Spp", "Gray Snapper", 
    "Great Barracuda", "Grunt spp", "Gulf Flounder", "Gulf Killifish", 
    "Gulf Pipefish", "Gulf Toadfish", "Halfbeak spp", "Hardhead Silverside", 
    "Harlequin Brittle Star", "Harris Mud Crab", "Highfin Blenny", 
    "Hogchoker", "Horseshoe Crab", "Iridescent Shrimp", "Jack spp", 
    "Jewel Cichlid", "Killifish spp", "Least Puffer", "Lesser Blue Crab", 
    "Lined Seahorse", "Lined Sole", "Lobate Mud Crab", "Longnose Spider Crab", 
    "Longsnout Seahorse", "Longtail Grass Shrimp", "Mangrove Gambusia", 
    "Mangrove Rivulus", "Manning Grass Shrimp", "Marsh Killifish", 
    "Marsh Shrimp", "Mayan Cichlid", "Mojarra spp", "Mud Crab spp", 
    "Mullet spp", "Needlefish spp", "Oyster Mud Crab", "Pearl Blenny", 
    "Pinfish", "Pink Shrimp", "Pink Shrimp spp", "Pipefish spp", 
    "Porgy spp", "Puffer spp", "Pugnose Pipefish", "Rainwater Killifish", 
    "Red-Algae Shrimp", "Redear Sardine", "Redfin Needlefish", 
    "Roughneck Shrimp", "Sailfin Molly", "Sailor's Choice", "Saltmarsh Mud Crab", 
    "Sargassum Fish", "Sargassum Pipefish", "Sargassum Shrimp", 
    "Sargassum Swimming Crab", "Say Mud Crab", "Schoolmaster Snapper", 
    "Sea Star spp", "Seabream", "Seahorse spp", "Sheepshead", 
    "Sheepshead Minnow", "Silver Jenny", "Silverside spp", "Slender Mojarra", 
    "Slender Sargassum Shrimp", "Small Spine Sea Star", "Smooth Mud Crab", 
    "Snapper spp", "Snapping Shrimp (A viridari)", "Snapping Shrimp (A. angulosus)", 
    "Snapping Shrimp spp", "Southern Pink Shrimp", "Southern Puffer", 
    "Southern Sennet", "Spaghetti Eel", "Speckled Worm Eel", 
    "Spider Crab spp", "Sponge Spider Crab", "Spotted Pink Shrimp", 
    "Spotted Whiff", "Squat Grass Shrimp", "Stone Crab", "Striped Mullet", 
    "Swimming Crab spp", "Timicu", "Tomtate", "Tripletail", "White Grunt", 
    "White Mullet", "Whitespotted Filefish", "Yellowfin Mojarra", 
    "Zostera Shrimp"), class = "factor"), num = c(0L, 1L, 0L, 
    4L, 2L, 0L, 0L, 0L, 4L, 0L, 5L, 24L, 0L, 0L, 0L, 0L, 1L, 
    5L, 0L, 2L, 3L, 0L, 0L, 38L, 25L, 0L, 14L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 1L, 9L, 0L, 5L, 20L, 10L, 0L, 17L, 0L, 0L, 0L, 
    66L, 2L, 64L, 0L, 5L, 4L, 0L, 12L, 49L, 0L, 0L, 2L, 0L, 2L, 
    0L, 0L, 0L, 0L, 0L, 1L, 4L, 0L, 1L, 4L, 0L, 0L, 2L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 16L, 12L, 12L, 0L, 0L, 26L, 2L, 
    0L, 0L)), class = "data.frame", row.names = c(NA, -90L))

这就是我开始一次为一个物种制作一个地块的方式:

# Select species
rain <- subset(data,common_name == "Rainwater Killifish", 
                   select = c(year, 
                              season, 
                              site, 
                              common_name,
                              num))

cdata2 <- ddply(rain, c("year", "season"), summarise,
                N    = length(num),
                n_mean = mean(num),
                n_median = median(num),
                sd   = sd(num),
                se   = sd / sqrt(N))

cdata2$year_season <- paste(cdata2$year, "_", cdata2$season, sep = "")

cdata2 <-cdata2 %>% mutate(year=ifelse(season=="wet",year+0.75,year+0.25))

ggplot(cdata2, aes(x = year, y = n_mean, color = season)) +
  annotate(geom = "rect", xmin = 2010, xmax = 2010.5, ymin = -Inf, ymax = Inf,
           fill = "lightblue", colour = NA, alpha = 0.4) +
  annotate(geom = "rect", xmin = 2013.5, xmax = 2014, ymin = -Inf, ymax = Inf,
           fill = "lightgreen", colour = NA, alpha = 0.4) +
  annotate(geom = "rect", xmin = 2017.5, xmax = 2018, ymin = -Inf, ymax = Inf,
           fill = "#E0E0E0", colour = NA, alpha = 0.4) +
  annotate(geom = "rect", xmin = 2011.5, xmax = 2012, ymin = -Inf, ymax = Inf,
           fill = "pink", colour = NA, alpha = 0.4) +
  annotate(geom = "rect", xmin = 2015.5, xmax = 2016, ymin = -Inf, ymax = Inf,
           fill = "pink", colour = NA, alpha = 0.4) +
  annotate(geom = "rect", xmin = 2018.5, xmax = 2019, ymin = -Inf, ymax = Inf,
           fill = "orange", colour = NA, alpha = 0.4) +
  geom_errorbar(aes(ymin=n_mean-se, ymax=n_mean+se), 
                width=.2, 
                color = "black") +
  geom_point(color = "black", 
             shape = 21, 
             size = 3,
             aes(fill = season)) +
  scale_fill_manual(values=c("white", "#C0C0C0")) + 
  scale_x_continuous(breaks=c(2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2018,2019,2020)) +
  theme(panel.border = element_rect(fill = NA, color = "black"),
        panel.background = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  labs(x="Year", y = "Mean count") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.y = element_text(size = 10, face = "bold")) +
  theme(axis.text.x = element_text(size = 10, face = "bold")) +
  theme(axis.title = element_text(size = 14, face = "bold"))

【问题讨论】:

    标签: r loops if-statement ggplot2 automation


    【解决方案1】:

    是的,这就是 for 循环的用途!你已经完成了所有的努力。现在只需粘贴您在循环中编写的代码并将common_name == "Rainwater Killifish" 替换为common_name == species

    allspecies <- unique(data$common_name)
    
    for(species in allspecies){
    
        ## insert your code here
        
        ggsave(paste0(species, ".png"))
    }
    

    【讨论】:

    • 太棒了!非常感谢!!作为奖励,有没有办法让 R 也打印通用名称作为每个物种的情节标题?
    • 不客气!是的,只需将ggtitle(species) + 添加到您的ggplot() 电话中即可。更多信息:sthda.com/english/wiki/…
    • 再次感谢!干杯! :D
    【解决方案2】:

    您可以将数据框嵌套到物种组,然后使用mutatemap 组合为每个物种组创建一个图。然后你可以使用deframe将名称和值列变成一个命名列表:

    library(tidyverse)
    plots <-
      data %>%
      nest(-common_name) %>%
      mutate(
        plt = data %>% map(possibly(~ {
          .x %>%
            ggplot(aes(year, num, color = site)) +
              geom_point()
        }, NA))
      ) %>%
      select(common_name, plt) %>%
      deframe()
    
    plots[["Mojarra spp"]]
    plots[["Rainwater Killifish"]]
    

    possibly 函数将返回 NA 以防绘图失败。 这是使用较新的 tidyverse 包,如 https://r4ds.had.co.nz/many-models.html#nested-data

    中所述

    【讨论】:

      猜你喜欢
      • 2021-11-03
      • 2021-03-28
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-29
      相关资源
      最近更新 更多