install.packages(“dplyr”)
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.6.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
edidiv <- read.csv("C:/Users/maquiroga/Documents/intro_to_R/CC-RBasics-master/CC-RBasics-master/edidiv.csv")
head(edidiv)
## organisationName gridReference year taxonName
## 1 Joint Nature Conservation Committee NT265775 2000 Sterna hirundo
## 2 Joint Nature Conservation Committee NT235775 2000 Sterna hirundo
## 3 Joint Nature Conservation Committee NT235775 2000 Sterna paradisaea
## 4 British Trust for Ornithology NT27 2000 Branta canadensis
## 5 British Trust for Ornithology NT27 2000 Branta leucopsis
## 6 The Wildlife Information Centre NT27S 2001 Turdus merula
## taxonGroup
## 1 Bird
## 2 Bird
## 3 Bird
## 4 Bird
## 5 Bird
## 6 Bird
tail(edidiv)
## organisationName gridReference year
## 25679 The Mammal Society NT278745 2016
## 25680 The Mammal Society NT277724 2016
## 25681 The Mammal Society NT266728 2016
## 25682 The Mammal Society NT270728 2016
## 25683 The Mammal Society NT257762 2016
## 25684 People's Trust for Endangered Species NT2372 2016
## taxonName taxonGroup
## 25679 Sciurus carolinensis Mammal
## 25680 Capreolus capreolus Mammal
## 25681 Sciurus carolinensis Mammal
## 25682 Oryctolagus cuniculus Mammal
## 25683 Vulpes vulpes Mammal
## 25684 Erinaceus europaeus Mammal
str(edidiv)
## 'data.frame': 25684 obs. of 5 variables:
## $ organisationName: Factor w/ 28 levels "BATS & The Millennium Link",..: 14 14 14 8 8 28 28 28 28 28 ...
## $ gridReference : Factor w/ 1938 levels "NT200701","NT200712",..: 1314 569 569 1412 1412 1671 1671 1671 1671 1671 ...
## $ year : int 2000 2000 2000 2000 2000 2001 2001 2001 2001 2001 ...
## $ taxonName : Factor w/ 1275 levels "Acarospora fuscata",..: 1126 1126 1127 192 193 1202 365 977 472 947 ...
## $ taxonGroup : Factor w/ 11 levels "Beetle","Bird",..: 2 2 2 2 2 2 2 2 2 2 ...
head(edidiv$taxonGroup)
## [1] Bird Bird Bird Bird Bird Bird
## 11 Levels: Beetle Bird Butterfly Dragonfly Flowering.Plants ... Mollusc
edidiv$taxonGroup = as.factor(edidiv$taxonGroup)
class(edidiv$taxonGroup)
## [1] "factor"
dim(edidiv)
## [1] 25684 5
summary(edidiv)
## organisationName gridReference
## Biological Records Centre :6744 NT2673 : 2741
## RSPB :5809 NT2773 : 2031
## Butterfly Conservation :3000 NT2873 : 1247
## Scottish Wildlife Trust :2070 NT2570 : 1001
## Conchological Society of Great Britain & Ireland:1998 NT27 : 888
## The Wildlife Information Centre :1860 NT2871 : 767
## (Other) :4203 (Other):17009
## year taxonName taxonGroup
## Min. :2000 Maniola jurtina : 1710 Butterfly :9670
## 1st Qu.:2006 Aphantopus hyperantus: 1468 Bird :7366
## Median :2009 Turdus merula : 1112 Flowering.Plants:2625
## Mean :2009 Lycaena phlaeas : 972 Mollusc :2226
## 3rd Qu.:2011 Aglais urticae : 959 Hymenopteran :1391
## Max. :2016 Aglais io : 720 Mammal : 960
## (Other) :18743 (Other) :1446
summary(edidiv$taxonGroup)
## Beetle Bird Butterfly Dragonfly
## 426 7366 9670 421
## Flowering.Plants Fungus Hymenopteran Lichen
## 2625 334 1391 140
## Liverwort Mammal Mollusc
## 125 960 2226
Beetle = filter(edidiv, taxonGroup == "Beetle")
Bird = filter(edidiv, taxonGroup == "Bird")
Butterfly = filter(edidiv, taxonGroup == "Butterfly")
Dragonfly = filter(edidiv, taxonGroup == "Dragonfly")
Flowering.Plants = filter(edidiv, taxonGroup == "Flowering.Plants")
Fungus = filter(edidiv, taxonGroup == "Fungus")
Hymenopteran = filter(edidiv, taxonGroup == "Hymenopteran")
Liverwort = filter(edidiv, taxonGroup == "Liverwort")
a = length(unique(Beetle$taxonName))
b = length(unique(Bird$taxonName))
c = length(unique(Butterfly$taxonName))
d = length(unique(Dragonfly$taxonName))
e = length(unique(Flowering.Plants$taxonName))
f = length(unique(Fungus$taxonName))
g = length(unique(Hymenopteran$taxonName))
h = length(unique(Liverwort$taxonName))
a
## [1] 37
b
## [1] 86
c
## [1] 25
d
## [1] 11
e
## [1] 521
f
## [1] 219
g
## [1] 112
h
## [1] 40
biodiv <- c(a,b,c,d,e,f,g,h)
names(biodiv) = c("Beetle","Bird","Butterfly","Dragonfly","Flowering.Plants","Fungus","Hymenopteran","Liverwort")
barplot(biodiv)
##### Como hay cosas que no están del todo bien, para solucionarlas se puede usar la función “help()”, para obtener ayuda con el diagrama de barras y ayuda con el trazado en general.
help("barplot")
## starting httpd help server ... done
help("par")
png("barplot.png", width=1600, height=600)
barplot(biodiv, xlab="Taxa", ylab="Number of species",space = c(0.2,0.2,0.2,0.2,0.2,0.2,0.2), ylim=c(0,600), cex.names= 0.45, cex.axis=0.8, cex.lab=1, main = "Riqueza de especies")
## Warning in space + width: longitud de objeto mayor no es múltiplo de la longitud
## de uno menor
## Warning in space + width: longitud de objeto mayor no es múltiplo de la longitud
## de uno menor
taxa = c("Beetle","Bird","Butterfly","Dragonfly","Flowering.Plants","Fungus","Hymenopteran","Liverwort")
taxa_f = factor(taxa)
richness = c(a, b, c, d, e, f, g, h)
biodata = data.frame(taxa_f, richness)
write.csv(biodata, file="biodata.csv")
png("barplot2.png", width = 1600, height = 600)
barplot(biodata$richness, names.arg = c("Beetle","Bird","Butterfly","Dragonfly","Flowering.Plants","Fungus","Hymenopteran","Liverwort"), xlab = "Taxa", ylab = "Number of species", ylim = c(0,600))
### RETO ##### ¿Puedes producir un diagrama de barras de la envergadura media de cada especie y guardarlo en tu computadora? ¿Cuál podría ser la función para calcular la media? Calcule la envergadura media de cada especie de ave. La función para hacer eso es simplemente: mean ()
sparrow <- mean(22, 24, 21)
kingfisher <- mean(26, 23, 25)
eagle <- mean(195, 201, 185)
hummingbird <- mean(8, 9, 9)
wingspan <- c(sparrow, kingfisher, eagle, hummingbird)
bird_sp <- c("sparrow", "kingfisher", "eagle", "hummingbird")
class(bird_sp)
## [1] "character"
bird_sp <- as.factor(bird_sp)
class(bird_sp)
## [1] "factor"
wings <- data.frame(bird_sp, wingspan)
png("wingspan_plot.png", width=800, height=600)
barplot(wings$wingspan, names.arg = wings$bird_sp, xlab = "Bird species", ylab = "Average wingspan (cm)", ylim = c(0, 200), col = "gold")