2022-10-07
2+2
## [1] 4
4*10
## [1] 40
sqrt(16)
## [1] 4
mean(c(2,3,5,8,9,5))
## [1] 5.333333
sd(c(2,3,5,8,9,5))
## [1] 2.73252
median(c(2,3,5,8,9,5))
## [1] 5
Grande quantidade de desenvolvedores (open-source)
Instalando e baixando pacotes (apenas na 1ª vez)
# install.packages('ggplot2')
# install.packages('dplyr')
# install.packages('raster')
# install.packages('sf')
library(ggplot2) library(dplyr) library(raster) library(sf) library(units)
| id | data | lat | lon | ordem | subordem | familia | nmin | nmax |
|---|---|---|---|---|---|---|---|---|
| 618 | 2009-08-01 00 00 00 | -25.050 | -47.87306 | Cetacea | Odontoceti | Delphinidae | 545 | 2180 |
| 32735 | 2012-11-04 11 18 00 | -30.768 | -49.17400 | Cetacea | Odontoceti | Delphinidae | 500 | 1200 |
| 32734 | 2012-11-04 11 00 00 | -30.809 | -49.15300 | Cetacea | Odontoceti | Delphinidae | 500 | 1200 |
| 32904 | 2014-06-04 08 21 00 | -24.353 | -43.62800 | Cetacea | Odontoceti | Delphinidae | 600 | 1300 |
| 618 | 2009-08-01 00 00 00 | -25.050 | -47.87306 | Cetacea | Odontoceti | Delphinidae | 545 | 2180 |
| 32735 | 2012-11-04 11 18 00 | -30.768 | -49.17400 | Cetacea | Odontoceti | Delphinidae | 500 | 1200 |
| 32734 | 2012-11-04 11 00 00 | -30.809 | -49.15300 | Cetacea | Odontoceti | Delphinidae | 500 | 1200 |
| 32904 | 2014-06-04 08 21 00 | -24.353 | -43.62800 | Cetacea | Odontoceti | Delphinidae | 600 | 1300 |
mam$data mam[2]
## Selecting by data
| data |
|---|
| 2021-10-16 20 10 00 |
| 2021-10-16 16 02 00 |
| 2021-10-10 23 04 00 |
| 2021-10-06 19 22 00 |
| 2021-10-03 23 29 00 |
| 2021-10-16 16 05 00 |
| 2022-07-12 18 05 00 |
| 2022-07-12 13 43 00 |
| 2021-10-06 09 53 00 |
| 2021-10-15 08 35 00 |
| 2021-10-16 20 10 00 |
| 2021-10-16 16 02 00 |
| 2021-10-10 23 04 00 |
| 2021-10-06 19 22 00 |
| 2021-10-03 23 29 00 |
| 2021-10-16 16 05 00 |
| 2022-07-12 18 05 00 |
| 2022-07-12 13 43 00 |
| 2021-10-06 09 53 00 |
| 2021-10-15 08 35 00 |
#Criando uma sequência 2:5
## [1] 2 3 4 5
#Criando uma sequência com passos definidos seq(from=1, to=10, by=2)
## [1] 1 3 5 7 9
mam[1:4,7:9]
| familia | nmin | nmax |
|---|---|---|
| Delphinidae | 1 | 1 |
| Delphinidae | 1 | 1 |
| Delphinidae | 1 | 1 |
| Delphinidae | 1 | 1 |
mam[1:2,c(7,2,3)]
mam[1:2,c('familia','lat','lon')]
| familia | lat | lon |
|---|---|---|
| Delphinidae | -25.91442 | -43.31745 |
| Delphinidae | -25.91580 | -43.29417 |
library(dplyr) mam<-mam %>% arrange(lat)
| id | data | lat | lon | ordem | subordem | familia | nmin | nmax |
|---|---|---|---|---|---|---|---|---|
| 618 | 2009-08-01 00 00 00 | -25.050 | -47.87306 | Cetacea | Odontoceti | Delphinidae | 545 | 2180 |
| 32735 | 2012-11-04 11 18 00 | -30.768 | -49.17400 | Cetacea | Odontoceti | Delphinidae | 500 | 1200 |
| 32734 | 2012-11-04 11 00 00 | -30.809 | -49.15300 | Cetacea | Odontoceti | Delphinidae | 500 | 1200 |
| 32904 | 2014-06-04 08 21 00 | -24.353 | -43.62800 | Cetacea | Odontoceti | Delphinidae | 600 | 1300 |
| 618 | 2009-08-01 00 00 00 | -25.050 | -47.87306 | Cetacea | Odontoceti | Delphinidae | 545 | 2180 |
| 32735 | 2012-11-04 11 18 00 | -30.768 | -49.17400 | Cetacea | Odontoceti | Delphinidae | 500 | 1200 |
| 32734 | 2012-11-04 11 00 00 | -30.809 | -49.15300 | Cetacea | Odontoceti | Delphinidae | 500 | 1200 |
| 32904 | 2014-06-04 08 21 00 | -24.353 | -43.62800 | Cetacea | Odontoceti | Delphinidae | 600 | 1300 |
library(dplyr) mam<-mam %>% filter(familia=='Kogiidae')
## Selecting by nmax
| id | data | lat | lon | ordem | subordem | familia | nmin | nmax |
|---|---|---|---|---|---|---|---|---|
| 31974 | 2017-04-08 11 35 00 | -25.2484 | -42.048 | Cetacea | Odontoceti | Kogiidae | 2 | 2 |
| 31974 | 2017-04-08 11 35 00 | -25.2484 | -42.048 | Cetacea | Odontoceti | Kogiidae | 2 | 2 |
mam<-mam %>% select(id, data)
| id | data |
|---|---|
| 59095 | 2021-10-16 20 10 00 |
| 58808 | 2022-07-12 18 05 00 |
| 58807 | 2022-07-12 13 43 00 |
| 59095 | 2021-10-16 20 10 00 |
| 58808 | 2022-07-12 18 05 00 |
| 58807 | 2022-07-12 13 43 00 |
mam%>%select(-nmin, -nmax)
## Selecting by familia
| id | data | lat | lon | ordem | subordem | familia |
|---|---|---|---|---|---|---|
| 36540 | 2019-09-16 09 42 00 | -26.80510 | -48.58400 | Cetacea | Odontoceti | Pontoporiidae |
| 628 | 2003-11-25 00 00 00 | -27.41000 | -48.56444 | Cetacea | Odontoceti | Pontoporiidae |
| 627 | 2003-11-23 00 00 00 | -27.41278 | -48.56500 | Cetacea | Odontoceti | Pontoporiidae |
| 626 | 2003-07-09 00 00 00 | -27.40667 | -48.56278 | Cetacea | Odontoceti | Pontoporiidae |
| 625 | 2002-06-28 00 00 00 | -27.39750 | -48.55556 | Cetacea | Odontoceti | Pontoporiidae |
| 624 | 1997-07-12 00 00 00 | -27.41472 | -48.54889 | Cetacea | Odontoceti | Pontoporiidae |
| 36540 | 2019-09-16 09 42 00 | -26.80510 | -48.58400 | Cetacea | Odontoceti | Pontoporiidae |
| 628 | 2003-11-25 00 00 00 | -27.41000 | -48.56444 | Cetacea | Odontoceti | Pontoporiidae |
| 627 | 2003-11-23 00 00 00 | -27.41278 | -48.56500 | Cetacea | Odontoceti | Pontoporiidae |
| 626 | 2003-07-09 00 00 00 | -27.40667 | -48.56278 | Cetacea | Odontoceti | Pontoporiidae |
| 625 | 2002-06-28 00 00 00 | -27.39750 | -48.55556 | Cetacea | Odontoceti | Pontoporiidae |
| 624 | 1997-07-12 00 00 00 | -27.41472 | -48.54889 | Cetacea | Odontoceti | Pontoporiidae |
summary(mam[3:8])
## lat lon ordem subordem ## Min. :-34.99 Min. :-52.08 Length:5251 Length:5251 ## 1st Qu.:-26.28 1st Qu.:-46.00 Class :character Class :character ## Median :-24.77 Median :-43.21 Mode :character Mode :character ## Mean :-25.77 Mean :-44.12 ## 3rd Qu.:-23.60 3rd Qu.:-42.07 ## Max. :-23.00 Max. :-36.97 ## familia nmin ## Length:5251 Min. : 1.00 ## Class :character 1st Qu.: 1.00 ## Mode :character Median : 2.00 ## Mean : 17.89 ## 3rd Qu.: 10.00 ## Max. :1000.00
#Soma sum(mam$nmin, na.rm=T)
## [1] 93935
#Média mean(mam$lat)
## [1] -25.77081
#Mediana median(mam$lat)
## [1] -24.7714
mam%>%summarise(latmedia=mean(lat),
nmax=sum(nmax),
n=n())
| latmedia | nmax | n |
|---|---|---|
| -25.77081 | 125940 | 5251 |
mam%>%
group_by(familia)%>%
summarise(latmedia=mean(lat),
nmax=sum(nmax, na.rm=T),
n=n())
| familia | latmedia | nmax | n |
|---|---|---|---|
| Balaenidae | -25.41938 | 11 | 6 |
| Balaenopteridae | -24.75227 | 2897 | 1371 |
| Delphinidae | -25.62586 | 120422 | 3394 |
| Physeteridae | -29.80460 | 2500 | 462 |
| Pontoporiidae | -27.30779 | 88 | 12 |
mam<-mam%>%mutate(data2=as.Date(data),
time=as.POSIXct(data, format="%Y-%m-%d %H %M %S"))
mam$month<-format(mam$time,'%b')
mam$month<-format(mam$time,'%m')
mam$year<-format(mam$time,'%Y')
mam$week<-format(mam$time, '%Y %V')
mam$idade<-Sys.time()-mam$time
hist(mam$lat)
ggplot(mam,aes(x=lat))+geom_histogram()
ggplot(mam,aes(x=lat))+
ggtitle('Distribuição da Latitude de Avistagens')+ #título do gráfico
geom_histogram(bins=50,fill='darkgreen')+#barras em verde escuro
theme_bw()+ #fundo branco
xlab('Latitude')+ #título do eixo x
ylab('Nº de Avistagens') #título do eixo y
ggplot(mam,aes(x=data2, y=nmax))+
geom_bar(stat='sum')+#barras em verde escuro
theme_bw()+ #fundo branco
xlab('Data')+ #título do eixo x
ylab('Nº de Avistagens') #título do eixo y
ggplot(mam,aes(x=lat, fill=familia))+
ggtitle('Distribuição da Latitude de Avistagens')+
geom_histogram(bins=50)+
xlab('Latitude')+
ylab('Nº de Avistagens')
ggplot(mam,aes(x=lat))+
ggtitle('Distribuição da Latitude de Avistagens')+ #título do gráfico
geom_histogram(bins=50)+#barras em verde escuro
theme_bw()+ #fundo branco
xlab('Latitude')+ #título do eixo x
ylab('Nº de Avistagens')+ #título do eixo y
facet_wrap(~familia, scales='free_y')
ggplot(mam,aes(x=familia,y=lat))+
geom_boxplot()+
ylab('Latitude')+
xlab(NULL)
mam_year<-mam%>%filter(!is.na(data))%>%
group_by(month, familia)%>%
summarise(n=sum(nmax))
ggplot(mam_year,aes(x=month, y=n, fill=familia))+
geom_bar(stat='identity')+
theme_bw()+
xlab('Data')+
ylab('Nº de Avistagens')
ggplot(mam,aes(x=lon,y=lat, color=familia, size=nmax))+
geom_point(alpha=.3)+
ylab('Latitude')+
xlab('Longitude')+
labs(size='Nº de Indivíduos',
fill='Família')
#Carregando um shapefile
states<-read_sf('simmam/states.shp')
plot(states)
plot(states["area"])
plot(states['area'])
<<<<<<< HEAD ## Unindo os polígonos
bra<-st_union(states) plot(bra)
mam_sf<-st_as_sf(mam, coords=c('lon','lat'), crs=4326)
plot(mam_sf)
mam_sf$distcosta<-st_distance(mam_sf,bra)
mam_sf$distkm<-as.numeric(mam_sf$distcosta)/1000
ggplot(mam_sf,aes(x=familia,y=distkm))+
geom_boxplot()+
xlab(NULL)+
ylab('Distância da Costa (km)')
ggplot()+ geom_sf(data=states)+ geom_sf(data=mam_sf,aes(color=familia))
range(mam$lon)
range(mam$lat)
ggplot()+
geom_sf(data=states)+
geom_sf(data=mam_sf,aes(color=familia, size=nmax), alpha=.3)+
coord_sf(xlim=c(-53.5, -36), ylim=c(-35.5,-23), expand=F)+
labs(color='Familia',
size='Nº de Indivíduos')
ggplot()+ geom_sf(data=states)+ coord_sf(xlim=c(-53.5, -36), ylim=c(-35.5,-23), expand=F)+ ## calculando as densidades stat_density_2d(data=mam,aes(x=lon, y=lat, fill=..level..),geom='polygon',alpha=.5)+ scale_fill_viridis_b() ## modificando a paleta de cores
grid<-st_make_grid(mam_sf, cellsize = 1) ggplot()+ geom_sf(data=states)+ geom_sf(data=grid, alpha=0)+ coord_sf(xlim=c(-53.5, -36), ylim=c(-35.5,-23), expand=F)
tab<-st_intersects(grid,mam_sf) grida<- st_sf(n = lengths(tab), geometry = st_cast(grid, "MULTIPOLYGON")) ggplot()+ geom_sf(data=grida%>%filter(n>0),aes(fill=log(n)))+ geom_sf(data=states)+ coord_sf(xlim=c(-53.5, -36), ylim=c(-35.5,-23), expand=F)+ scale_fill_viridis_b()
bat<-raster('simmam/batimetria.tif')
plot(bat)
# Criando um raster booleano plat<-bat>=-200 plot(plat)
# Obtendo a profundidade mam_sf$prof<-extract(bat, mam_sf) # Separando dados da plataforma continental mam_sf$plat<-extract(plat, mam_sf) ggplot(mam_sf,aes(x=prof,fill=as.factor(plat)))+geom_histogram()