r语言 - 自动为组中的每个级别制作时间序列图



我最初的问题可以在这里看到(为一个组中的每个关卡自动绘制一个ggplot),但我认为我应该用不同的方式来回答这个问题,而不是"如何"。问题来纠正我的错误尝试。

我想使创建像下面这样的时间序列图的过程更快/自动(即,不需要用户一次输入一个物种名称)。也许加上一个"如果";循环。告诉R循环遍历数据中所有唯一的通用名称,并使用下面的代码(带有"common_name")打印(或保存到png)一个图。每个物种作为各自情节的标题)。如果没有足够的数据用于绘图,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"))

是的,这就是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"))
}

您可以将数据框架嵌套到物种组中,然后使用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

中所述。

最新更新