使用 ggplot 循环

Posted

技术标签:

【中文标题】使用 ggplot 循环【英文标题】:Loops with ggplot 【发布时间】:2022-01-11 14:37:27 【问题描述】:

我想查看我的个体鱼数据中的模式。我使用本文中提供的代码在 ggplot 中为个人制作了一个工作图。在这里,我选择列名为Transmitter 的每条鱼。这意味着我必须为每个图选择发射器,这效率不高。我有 10 个唯一的发射器名称,我想在 10 个图中可视化它们,但我想循环这个,所以我不必复制粘贴所有内容。我怎样才能自动化呢?额外问题:如何在 R 中可视化 10 个图(左、右 2 个图和彼此下方 5 个),以便我可以快速扫描我的图?提前致谢!

情节:[![A69-1602-59767][1]][1] [![A69-1602-59768][2]][2]

我使用的 ggplot 代码:

library(spatialrisk)
library(ggplot2)

pattern2     <- Brpattern[Brpattern$Batch.location == "Medemblik",]
pattern2 <- points_in_circle(pattern2, lat_center = 52.76098, lon_center = 5.12172, lon = Longitude, lat = Latitude, radius = 1e6)
pattern2$distance_m <- pattern2$distance_m/1000
colnames(pattern2)[which(names(pattern2) == "distance_m")] <- "Distance (km)"

### Fish selection
patternME <- pattern2[pattern2$Transmitter == "A69-1602-59768",]
ggplot(patternME, aes(Date, `Distance (km)`))+
  geom_point(aes(colour = Location.Receiver), size=3)+
  scale_x_date(date_breaks = "months", date_labels = "%m-%Y")+
  ggtitle("Batch location Medemblik") + ylab("Distance from batch location (km)") + labs(colour ="A69-1602-59768")+
  theme(plot.title = element_text(hjust = 0.5))

我的数据:

structure(list(Day = c("29", "30", "31", "01", "02", "03", "04", 
"05", "06", "07", "08", "09", "10", "11", "12", "13", "16", "19", 
"20", "21", "22", "23", "03", "24", "07", "09", "11", "13", "15", 
"16", "17", "18", "20", "21", "08", "24", "25", "26", "27", "28", 
"29", "30", "31", "01", "02", "03", "04", "05", "06", "07", "08", 
"09", "10", "11", "12", "13", "16", "19", "20", "21", "22", "23", 
"30", "01", "02", "03", "04", "05", "22", "24", "25", "26", "27", 
"28", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", 
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", 
"22", "08", "25", "01", "02"), Month = c("12", "12", "12", "01", 
"01", "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", 
"01", "01", "01", "01", "01", "01", "01", "02", "02", "03", "03", 
"03", "03", "03", "03", "03", "03", "03", "03", "04", "12", "12", 
"12", "12", "12", "12", "12", "12", "01", "01", "01", "01", "01", 
"01", "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", 
"01", "01", "01", "01", "02", "02", "02", "02", "02", "02", "02", 
"02", "02", "02", "02", "03", "03", "03", "03", "03", "03", "03", 
"03", "03", "03", "03", "03", "03", "03", "03", "03", "03", "03", 
"03", "03", "03", "03", "04", "03", "04", "04"), Year = c("2019", 
"2019", "2019", "2020", "2020", "2020", "2020", "2020", "2020", 
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020", 
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020", 
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020", 
"2020", "2020", "2019", "2019", "2019", "2019", "2019", "2019", 
"2019", "2019", "2020", "2020", "2020", "2020", "2020", "2020", 
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020", 
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020", 
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020", 
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020", 
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020", 
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020", 
"2020", "2020", "2020"), Location.Receiver = c("Ketelmeer 2 (midden) ", 
"Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", 
"Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", 
"Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", 
"Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", 
"Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", 
"Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", 
"Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", 
"Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", 
"Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", 
"Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", 
"Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", "Ketelmeer 2 (midden) ", 
"Ketelmeer 2 (midden) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", "Ketelmeer 3 (noord) ", 
"Ijssel Ijssel", "Ijssel Ijssel", "Ijssel Ijssel"), Transmitter = c("A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759", "A69-1602-59759", 
"A69-1602-59759", "A69-1602-59759", "A69-1602-59759"), Batch.location = c("Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik", "Medemblik", 
"Medemblik", "Medemblik", "Medemblik", "Medemblik"), BatchNr = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Latitude = c(52.60896, 
52.60896, 52.60896, 52.60896, 52.60896, 52.60896, 52.60896, 52.60896, 
52.60896, 52.60896, 52.60896, 52.60896, 52.60896, 52.60896, 52.60896, 
52.60896, 52.60896, 52.60896, 52.60896, 52.60896, 52.60896, 52.60896, 
52.60896, 52.60896, 52.60896, 52.60896, 52.60896, 52.60896, 52.60896, 
52.60896, 52.60896, 52.60896, 52.60896, 52.60896, 52.60896, 52.61161, 
52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 
52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 
52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 
52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 
52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 
52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 
52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 
52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 
52.61161, 52.61161, 52.61161, 52.61161, 52.61161, 52.58222, 52.58222, 
52.58222), Longitude = c(5.65573, 5.65573, 5.65573, 5.65573, 
5.65573, 5.65573, 5.65573, 5.65573, 5.65573, 5.65573, 5.65573, 
5.65573, 5.65573, 5.65573, 5.65573, 5.65573, 5.65573, 5.65573, 
5.65573, 5.65573, 5.65573, 5.65573, 5.65573, 5.65573, 5.65573, 
5.65573, 5.65573, 5.65573, 5.65573, 5.65573, 5.65573, 5.65573, 
5.65573, 5.65573, 5.65573, 5.66058, 5.66058, 5.66058, 5.66058, 
5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 
5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 
5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 
5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 
5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 
5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 
5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 
5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 5.66058, 
5.66058, 5.66058, 5.86477, 5.86477, 5.86477), Date = structure(c(18259, 
18260, 18261, 18262, 18263, 18264, 18265, 18266, 18267, 18268, 
18269, 18270, 18271, 18272, 18273, 18274, 18277, 18280, 18281, 
18282, 18283, 18284, 18295, 18316, 18328, 18330, 18332, 18334, 
18336, 18337, 18338, 18339, 18341, 18342, 18360, 18254, 18255, 
18256, 18257, 18258, 18259, 18260, 18261, 18262, 18263, 18264, 
18265, 18266, 18267, 18268, 18269, 18270, 18271, 18272, 18273, 
18274, 18277, 18280, 18281, 18282, 18283, 18284, 18291, 18293, 
18294, 18295, 18296, 18297, 18314, 18316, 18317, 18318, 18319, 
18320, 18322, 18323, 18324, 18325, 18326, 18327, 18328, 18329, 
18330, 18331, 18332, 18333, 18334, 18335, 18336, 18337, 18338, 
18339, 18340, 18341, 18342, 18343, 18360, 18346, 18353, 18354
), class = "Date"), `Distance (km)` = c(39.8114372187258, 39.8114372187258, 
39.8114372187258, 39.8114372187258, 39.8114372187258, 39.8114372187258, 
39.8114372187258, 39.8114372187258, 39.8114372187258, 39.8114372187258, 
39.8114372187258, 39.8114372187258, 39.8114372187258, 39.8114372187258, 
39.8114372187258, 39.8114372187258, 39.8114372187258, 39.8114372187258, 
39.8114372187258, 39.8114372187258, 39.8114372187258, 39.8114372187258, 
39.8114372187258, 39.8114372187258, 39.8114372187258, 39.8114372187258, 
39.8114372187258, 39.8114372187258, 39.8114372187258, 39.8114372187258, 
39.8114372187258, 39.8114372187258, 39.8114372187258, 39.8114372187258, 
39.8114372187258, 39.9833461407132, 39.9833461407132, 39.9833461407132, 
39.9833461407132, 39.9833461407132, 39.9833461407132, 39.9833461407132, 
39.9833461407132, 39.9833461407132, 39.9833461407132, 39.9833461407132, 
39.9833461407132, 39.9833461407132, 39.9833461407132, 39.9833461407132, 
39.9833461407132, 39.9833461407132, 39.9833461407132, 39.9833461407132, 
39.9833461407132, 39.9833461407132, 39.9833461407132, 39.9833461407132, 
39.9833461407132, 39.9833461407132, 39.9833461407132, 39.9833461407132, 
39.9833461407132, 39.9833461407132, 39.9833461407132, 39.9833461407132, 
39.9833461407132, 39.9833461407132, 39.9833461407132, 39.9833461407132, 
39.9833461407132, 39.9833461407132, 39.9833461407132, 39.9833461407132, 
39.9833461407132, 39.9833461407132, 39.9833461407132, 39.9833461407132, 
39.9833461407132, 39.9833461407132, 39.9833461407132, 39.9833461407132, 
39.9833461407132, 39.9833461407132, 39.9833461407132, 39.9833461407132, 
39.9833461407132, 39.9833461407132, 39.9833461407132, 39.9833461407132, 
39.9833461407132, 39.9833461407132, 39.9833461407132, 39.9833461407132, 
39.9833461407132, 39.9833461407132, 39.9833461407132, 53.9604508112108, 
53.9604508112108, 53.9604508112108)), row.names = c(570298L, 
565084L, 563855L, 530554L, 552942L, 525810L, 552035L, 539968L, 
560812L, 529808L, 581237L, 567584L, 532850L, 578746L, 528980L, 
563420L, 576765L, 581972L, 571917L, 539528L, 549080L, 543315L, 
558992L, 562414L, 570063L, 581171L, 540686L, 580895L, 578984L, 
537421L, 566652L, 575695L, 560280L, 527067L, 540127L, 567792L, 
528656L, 549569L, 565553L, 560968L, 539858L, 572394L, 576133L, 
576947L, 534319L, 543011L, 528409L, 557659L, 575278L, 541422L, 
573611L, 531816L, 561909L, 567161L, 526719L, 526669L, 552503L, 
579272L, 570394L, 576650L, 556779L, 559094L, 553772L, 531240L, 
561776L, 581170L, 545572L, 548139L, 580422L, 552737L, 538357L, 
574885L, 562474L, 549462L, 556328L, 533075L, 560331L, 543957L, 
560719L, 529318L, 534122L, 567029L, 532939L, 525323L, 549903L, 
567378L, 581894L, 528378L, 543621L, 539881L, 556999L, 572109L, 
547942L, 537720L, 545126L, 547950L, 537362L, 576813L, 532197L, 
548260L), class = "data.frame")


  [1]: https://i.stack.imgur.com/weaK2.png
  [2]: https://i.stack.imgur.com/Yyrp9.png

【问题讨论】:

能不能把points_in_circle()这个函数加进去? @Peter 它来自spatialrisk 包。你仍然是对的 - 好问题包括所有使用的非基础 R 库。 我编辑我的问题。应该是包spatialrisk patternDO 在哪里定义?您能否在新的 R 实例中检查您的问题并确认它是可重现的,它可能需要 Transmitter == "A69-1602-59768" 的值。此外,最好从问题中生成您需要的 10 个绘图中的至少两个。 我删除了那行,很抱歉没有必要。我对 R 和这个社区还是新手,必须找到使用这个网站的方式。谢谢你的耐心:)! 【参考方案1】:

您可以通过Transmittersplit 数据并创建绘图列表。

library(spatialrisk)
library(ggplot2)

plot_data <- function(data, title) 
  
ggplot(data, aes(Date, `Distance (km)`))+
  geom_point(aes(colour = Location.Receiver), size=3)+
  scale_x_date(date_breaks = "months", date_labels = "%m-%Y")+
  ggtitle("Batch location Medemblik") + 
    ylab("Distance from batch location (km)") + 
    labs(colour = title)+
  theme(plot.title = element_text(hjust = 0.5))


list_of_plots <- purrr::imap(split(pattern2, pattern2$Transmitter), plot_data)

可以通过[[ 访问各个地块,即list_of_plots[[1]] 用于第一个地块,list_of_plots[[2]] 用于第二个地块,依此类推。

【讨论】:

这就是我想象的样子!但是,似乎每个情节都是一样的。发射器名称变化很好,但绘制的点不正确 要使其工作,您只需将patternME &lt;- pattern2[pattern2$Transmitter == "A69-1602-59768",] 更改为patternME &lt;- pattern2[pattern2$Transmitter == unique(pattern2$Transmitter)[x],],然后遍历x。作为旁注,您可能想查看cowplot 库。 你是什么意思:遍历X?我还需要手动填写发射器名称来代替 x 吗? @DJ 你能解释一下迭代x是什么意思吗?根据您的建议,我无法使其工作。

以上是关于使用 ggplot 循环的主要内容,如果未能解决你的问题,请参考以下文章

使用 for 循环保存多个 ggplots

在ggplot中循环对象

R语言使用for循环嵌套ggplot2可视化输出多个可视化结果实战

"for" 循环只添加最后的 ggplot 层

使用循环在 ggplot 中创建具有不同 Y 轴值的多个图

带有 ggplots 的 for 循环生成具有相同值但标题不同的图形