如何使用 ggplot 在 R 中为多个物种绘制平均 CPUE 图表

3

我试图找出按年份排名前五种最丰富物种的平均CPUE的最佳绘图方法。我能够将它们全部分开绘制,但这需要大量代码并且会变得混乱,我无法想出如何将它们全部绘制在同一张图中。

这是我的数据子集

structure(list(year = c(2011, 2016, 2016, 2009, 2016, 2016, 2011, 
                               2009, 2014, 2008, 2011, 2016, 2012, 2017, 2008, 2009, 2018, 2018, 
                               2016, 2012, 2013, 2012, 2016, 2013, 2017, 2012, 2016, 2009, 2008, 
                               2011, 2010, 2014, 2015, 2015, 2017, 2015, 2013, 2014, 2008, 2013, 
                               2018, 2013, 2013, 2009, 2009, 2010, 2017, 2010, 2008, 2013, 2013, 
                               2018, 2011, 2015, 2009, 2010, 2013, 2008, 2008, 2010, 2008, 2010, 
                               2011, 2015, 2008, 2012, 2016, 2016, 2008, 2018, 2012, 2013, 2012, 
                               2016, 2016, 2014, 2010, 2012, 2011, 2013, 2015, 2015, 2009, 2010, 
                               2009, 2008, 2016, 2017, 2008, 2012, 2015, 2008, 2016, 2017, 2014, 
                               2009, 2009, 2011, 2012, 2012), Commonname = c("Grass Porgy", 
                                                                             "Sand Perch", "Lane Snapper", "Scallops", "Striped Burrfish", 
                                                                             "Dusky Pipefish", "Eucinostomus", "Scallops", "Dusky Pipefish", 
                                                                             "Gulf Toadfish", "White Grunt", "Black Sea Bass", "Silver Perch", 
                                                                             "Eucinostomus", "Pigfish", "Banded Blenny", "Black Sea Bass", 
                                                                             "Spotted Seatrout", "Bay Scallop", "Fringed Filefish", "Planehead Filefish", 
                                                                             "Pigfish", "Grass Porgy", "Bighead Searobin", "Lane Snapper", 
                                                                             "Scallops", "Southern Puffer", "Dusky Pipefish", "Pigfish", "Pinfish", 
                                                                             "Pink Shrimp", "Spotted Seatrout", "Pink Shrimp", "Inshore Lizardfish", 
                                                                             "Bay Scallop", "Lined Sole", "White Grunt", "Spotted Seatrout", 
                                                                             "Gulf Toadfish", "Lined Sole", "Pinfish", "Gulf Toadfish", "Pinfish", 
                                                                             "Pigfish", "Fringed Filefish", "Fringed Filefish", "Atlantic Calico Scallop", 
                                                                             "Eucinostomus", "Pinfish", "Pink Shrimp", "Inshore Lizardfish", 
                                                                             "Portunus Crabs", "Scallops", "Dusky Pipefish", "Fringed Filefish", 
                                                                             "Pigfish", "Planehead Filefish", "Pigfish", "Silver Perch", "Fringed Pipefish", 
                                                                             "Spottail Pinfish", "Scrawled Cowfish", "Ocellated Moray", "Spottail Pinfish", 
                                                                             "Planehead Filefish", "White Grunt", "Spottail Pinfish", "Banded Blenny", 
                                                                             "Pink Shrimp", "Pigfish", "Hogfish", "Fringed Pipefish", "Grass Porgy", 
                                                                             "Gulf Pipefish", "Bay Scallop", "Orange Filefish", "Scallops", 
                                                                             "Polka-dot Batfish", "Gulf Toadfish", "Scallops", "Gulf Toadfish", 
                                                                             "Pigfish", "Silver Perch", "White Grunt", "Spottail Pinfish", 
                                                                             "Striped Burrfish", "Bandtail Puffer", "Scrawled Cowfish", "Chain Pipefish", 
                                                                             "Portunus Crabs", "Gulf Toadfish", "Scallops", "Dusky Pipefish", 
                                                                             "Spottail Pinfish", "Inshore Lizardfish", "Striped Burrfish", 
                                                                             "Inshore Lizardfish", "Spottail Pinfish", "Planehead Filefish", 
                                                                             "Banded Blenny"), CPUE = c(0.134916351861846, 0.149907057624273, 
                                                                                                        0.134916351861846, 0.404749055585537, 0.134916351861846, 1.19925646099418, 
                                                                                                        0.134916351861846, 0.134916351861846, 0.539665407447383, 0.224860586436409, 
                                                                                                        0.404749055585537, 2.02374527792769, 1.34916351861846, 0.94441446303292, 
                                                                                                        0.224860586436409, 0.299814115248546, 0.149907057624273, 0.404749055585537, 
                                                                                                        0.149907057624273, 1.18051807879115, 2.02374527792768, 3.77765785213168, 
                                                                                                        0.963688227584612, 0.168645439827307, 0.337290879654614, 0.674581759309228, 
                                                                                                        0.168645439827307, 2.39851292198837, 0.674581759309228, 12.2923787251904, 
                                                                                                        0.168645439827307, 0.299814115248546, 0.269832703723691, 0.134916351861846, 
                                                                                                        2.02374527792768, 0.134916351861846, 0.134916351861846, 0.134916351861846, 
                                                                                                        0.269832703723691, 0.269832703723691, 1.51780895844576, 0.809498111171074, 
                                                                                                        1.07933081489477, 0.134916351861846, 0.134916351861846, 0.134916351861846, 
                                                                                                        0.134916351861846, 0.134916351861846, 0.674581759309228, 0.269832703723691, 
                                                                                                        0.149907057624273, 0.149907057624273, 0.224860586436409, 1.648977633867, 
                                                                                                        0.599628230497092, 1.4840798704803, 0.192737645516922, 0.674581759309228, 
                                                                                                        0.269832703723691, 1.04934940336991, 2.02374527792769, 0.149907057624273, 
                                                                                                        0.134916351861846, 0.134916351861846, 5.26173772261198, 0.404749055585537, 
                                                                                                        4.34730467110392, 0.674581759309228, 0.134916351861846, 0.599628230497092, 
                                                                                                        1.07933081489477, 0.245302457930628, 0.269832703723691, 0.134916351861846, 
                                                                                                        0.449721172872819, 0.134916351861846, 0.269832703723691, 0.134916351861846, 
                                                                                                        1.79888469149128, 0.674581759309228, 0.134916351861846, 1.34916351861846, 
                                                                                                        0.269832703723691, 1.34916351861846, 0.134916351861846, 0.134916351861846, 
                                                                                                        0.149907057624273, 0.134916351861846, 0.404749055585537, 0.134916351861846, 
                                                                                                        1.4840798704803, 1.34916351861846, 2.24860586436409, 0.168645439827307, 
                                                                                                        0.269832703723691, 0.149907057624273, 0.134916351861846, 4.04749055585537, 
                                                                                                        0.149907057624273, 0.674581759309228)), row.names = c(1:200), class = "data.frame")


我在整个数据集上使用了这段代码,提取了每个物种的平均CPUE值,然后使用ggplot得到了下面的图形。

pinfish <- df %>% 
  filter( Commonname == "Pinfish") %>% 
  group_by(year) %>% 
  summarise(CPUE = mean(CPUE))
pinfish

ggplot(pinfish, aes(x=year, y= CPUE)) + 
  geom_line(aes()) +
  labs(title = "Annual Mean CPUE of Pinfish", y = "Mean CPUE (# fish/100m2)", x = "Date")+
  theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                     panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
                     axis.text.x = element_text(angle = 0))

[![enter image description here][1]][1]

有没有一种方法能够创建一个图表,展示出数据集中前五个最丰富物种的平均CPUE,而不需要像上面手动分离这些物种?这些是否可以合并到一个具有多条线的图表中或分成并排的单独图表呢?

我通过excel过滤数据找到了前五个最丰富的物种,但我很想知道是否有一种在R中快速过滤数据的方法?使用此数据子集可能无法获得相同的结果,但如果您能提供代码来提取它,那将是非常好的。

我在excel中检测到的前5个物种是: pinfish, pigfish, spottail pinfish, black sea bass和dusky pipefish。

非常感谢您提供的任何帮助!


我们如何看出数据中哪些物种是常见的?行名提供了什么信息? - Ahorn
1
行名是在我对数据进行子样本抽取时生成的,那些只是它为随机样本选择的行。我会将它们删除以消除混淆。 - user13314714
没问题。请看下面的答案。这是你要找的吗? - Ahorn
1个回答

2
根据所有年份的平均CPUE值获取最常见的物种。
library(dplyr)
library(ggplot2)

most_common <- df %>%
  group_by(Commonname) %>% 
  summarise(mean_cpue = mean(CPUE)) %>% 
  ungroup() %>% 
  arrange(desc(mean_cpue)) %>% 
  top_n(5) %>% 
  pull(Commonname)

绘制最常见的物种:

df %>% 
  group_by(Commonname, year) %>% 
  summarise(mean_CPUE_year = mean(CPUE)) %>% 
  filter(Commonname %in% most_common) %>% 
  ggplot(aes(x=year, y= mean_CPUE_year)) + 
  geom_line(aes()) +
  labs(title = "Annual Mean CPUE of Pinfish", y = "Mean CPUE (# fish/100m2)", x = "Date")+
  theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                     panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
                     axis.text.x = element_text(angle = 0)) +
  facet_wrap(~Commonname)

enter image description here


1
谢谢您的回答。第一部分非常适合查找最常见的物种。第二段代码可以运行,但是与我上面展示的例子中的平滑线不同,它会产生这样的结果https://photos.google.com/search/_tra_/photo/AF1QipN9hl-ylNsLUAnJd87liMhft0g8oOPEh_0bj_62 - user13314714
2
没关系。这个链接对我也不起作用。我已经在上面添加了图表,你看到的是相同的图表吗?你所说的“平滑”是什么意思?由于每年只有一个观察结果,而且只有几年的数据,因此构建的线条可能会很生硬。或者我漏掉了什么? - Ahorn
2
“smooth”这个词用得不太准确。我想要的是你上面图表所展示的内容。但是当我运行代码时,它画出来的线条与预期不同。我会尝试将图片添加到我的原始帖子中,以便向您展示我的意思。 - user13314714
2
我明白了。我忘记了你对每年物种的平均值。我已经更新了上面绘图的代码。现在这是否给出了与我的答案相似的图形? - Ahorn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接