簡體   English   中英

如何在 R 中使用 ggplot plot 表示多個物種的 CPUE

[英]How to plot mean CPUE for multiple species with ggplot in R

我試圖找出 plot 平均 CPUE 的最佳方法,用於每年最豐富的 5 個物種。 我能夠將這些 plot 分開,但它需要大量代碼並且變得混亂,我不知道如何將它們全部放在同一個數字上。

這是我的數據的一個子集

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))

[![在此處輸入圖片描述][1]][1]

是否可以創建一個 plot 的平均 CPUE 用於我的數據集中最豐富的 5 個物種,而不必像我上面那樣手動分離這些物種中的每一個? 這些是否可以組合成一個 plot 與多行或並排成單獨的圖?

我通過excel過濾數據找到了前5個最豐富的物種,但我很好奇在R中是否有一種快速過濾的方法? 你可能不會用這個數據子集得到相同的結果,但是如果你能提供一個可以拉它的代碼,那就太好了

我在 excel 中檢測到的前 5 個物種是:pinfish、pigfish、spottail pinfish、black sea bass 和 darky pipefish。

提前感謝您的幫助!

根據所有年份的平均 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)

Plot 最常見的物種:

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)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM