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

我试图找出最好的方法来按年绘制5个最丰富物种的平均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

是否可以用我的数据集中前5个最丰富物种的平均CPUE来创建一个图,而不必像上面一样手动分离每个物种?是否可以将这些合并成一条多条线的图或并排的单独图?

我通过excel筛选数据找到了前5个最丰富的物种,但是我很好奇是否有一种快速的方法可以在R中进行筛选吗?对于这部分数据,您可能不会获得相同的结果,但是如果您可以提供将其提取的代码,那将会很棒

我在excel中检测到的前5种是:pin鱼,猪鱼,点尾pin鱼,黑鲈鱼和暗淡的pipe鱼。

在此先感谢您的帮助!

评论
  • 凹凸萌
    凹凸萌 回复

    根据多年来的平均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 %>% 
      filter(Commonname %in% most_common) %>% 
      ggplot(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)) +
      facet_wrap(~Commonname)