first commit

master
Marvin Johanning 2022-07-17 19:44:44 +02:00
commit 89d4e756c0
4 changed files with 320 additions and 0 deletions

120
Untitled.R Normal file
View File

@ -0,0 +1,120 @@
install.packages("ggmap")
install.packages("sf")
install.packages("ggrepel")
library("ggmap")
library(tidyverse)
library(sf)
library(mapview)
library(dplyr)
library(ggrepel)
register_google(key = "[AIzaSyDTykAdRVF2lO-7JKsLLZwoP534vRqyqI4]")
#myLocation<-c(8.50, 52.04, 8.61, 52)
#myLocation<-c(7, 47.75, 8.5, 48.25)
myLocation<-c(8.50, 52, 8.61, 52.04)
bw_map <- get_googlemap("bielefeld stauteich III", zoom = 14, maptype = "satellite")
setwd("/Users/mjo-air/Programming/R")
locations <- read.csv("ornitho.txt", sep = "\t")[,c('COORD_LAT', 'COORD_LON', 'TOTAL_COUNT', 'NAME_SPECIES', 'PLACE')]
locations <- locations %>% slice(-1)
print(locations)
locations <- read.csv("birding.csv", stringsAsFactors = F, na.strings="`")[,c('Location.1', 'Longitude', 'Latitude', 'Number', 'Common')]
locations <- read.csv("birding.csv", stringsAsFactors = F, na.strings="`")[,c('Location.1', 'Longitude', 'Latitude', 'Common')]
locations$Number<-gsub(">","",as.character(locations$Number))
locations$Number<-gsub("~","",as.character(locations$Number))
locations <- locations %>% mutate_at(4, ~replace_na(.,0))
locs <- as_tibble(locations)
locs <- locs %>% mutate_at(4, ~replace_na(.,0))
print(locs)
locs$COORD_LON %<>% as.double
locs$COORD_LAT %<>% as.double
locs$TOTAL_COUNT %<>% as.integer
locs$Number %<>% as.integer
locations_sf <- st_as_sf(locs, coords = c("COORD_LON", "COORD_LAT"), crs = 4326)
locations_sf <- st_as_sf(per_location, coords = c("Longitude", "Latitude"), crs = 4326)
mapview(locations_sf, zcol="sum")
per_location_no_dup <- locs[!duplicated(locs$Location.1),]
per_location_no_dup <- unique(locs)
per_location <- locs %>%
group_by(Location.1, Longitude, Latitude) %>%
summarise(sum = n()) %>%
arrange(desc(sum))
per_location <- per_location_no_dup %>%
group_by(Location.1, Longitude, Latitude) %>%
summarise(sum = n()) %>%
arrange(desc(sum))
per_location <- locs %>%
group_by(Location.1, Longitude, Latitude) %>%
summarise(sum = sum(Number)) %>%
arrange(desc(sum))
per_location[!duplicated(per_location$Common),]
print(per_location)
print(per_location_no_dup)
ggmap(bw_map) +
geom_point(data = per_location,
aes(x = Longitude, y = Latitude, size=sum),
color = "red", alpha = 0.5) +
geom_text_repel(data = per_location, aes(x = Longitude, y = Latitude, label = Location.1)) +
guides(color = guide_legend(override.aes = list(size = 6))) +
scale_size_continuous(range = c(2, 9))
barplot(height=per_location$sum, names=per_location$Location.1, las=2)
barplot(data$average , border=F , names.arg=data$name ,
las=2 ,
col=c(rgb(0.3,0.1,0.4,0.6) , rgb(0.3,0.5,0.4,0.6) , rgb(0.3,0.9,0.4,0.6) , rgb(0.3,0.9,0.4,0.6)) ,
ylim=c(0,13) ,
main="" )
ggmap(bw_map) +
geom_point(data = locs, aes(x = lon, y = lat))
lon <- distinct(data, Longitude)
lat <- distinct(data, Latitude)
locs %>%
group_by(Location.1) %>%
summarise(across(everything(), sum))
mapview(c(lon, lat))
locations_sf <- st_as_sf(locations, coords = c("lon", "lat"), crs = 4326)
per_source <- locs %>%
group_by(TOTAL_COUNT) %>%
summarise(count = n()) %>%
arrange(desc(count))
print(per_source)
print(data)
retval <- subset(data, Location.2 == "Heeper Fichten")
print(retval$Common)

47
bird_per_loc_scythe.R Normal file
View File

@ -0,0 +1,47 @@
library("ggmap")
library(tidyverse)
library(sf)
library(mapview)
library(dplyr)
library(ggrepel)
#Set working directory
setwd("/Users/mjo-air/Programming/R")
#Choose Scythebird export
locations <- read.csv(file=file.choose(), stringsAsFactors = F)[,c('Location.1', 'Common', 'Number')]
#Remove approximate, bigger than etc. signs
locations$Number<-gsub(">","",as.character(locations$Number))
locations$Number<-gsub("~","",as.character(locations$Number))
#Do a tibblings
locations <- as_tibble(locations)
#Change data types from character to double and integer
locations$Number %<>% as.integer
#Total number of birds by species per location
per_location_by_species <- locations %>%
group_by(Common, Location.1) %>%
summarise(sum = sum(Number)) %>%
arrange(desc(sum))
per_location_by_species <- subset(per_location_by_species, Common!="Stockente")
#Bar plot
ggplot(per_location_by_species, aes(sum, Common, fill = Location.1)) +
geom_bar(stat="identity", position = "dodge") +
labs(title="Number of birds per location")
print(per_location_by_species)
# Create mapview map
locations_sf <- st_as_sf(per_location, coords = c("COORD_LON", "COORD_LAT"), crs = 4326)
mapview(locations_sf, zcol="sum")
print(locations)

88
birding_trip_ornitho.R Normal file
View File

@ -0,0 +1,88 @@
library("ggmap")
library(tidyverse)
library(sf)
library(mapview)
library(dplyr)
library(ggrepel)
library(magrittr)
#Google API key
register_google(key = "")
#Google Maps view
stauteich_3 <- get_googlemap("bielefeld stauteich III", zoom = 14, maptype = "satellite")
#Set working directory
setwd("/Users/mjo-air/Programming/R")
#Choose Ornihto export
locations <- read.csv(file=file.choose(), sep = "\t")[,c('COORD_LAT', 'COORD_LON', 'TOTAL_COUNT', 'NAME_SPECIES', 'PLACE')]
#Remove first row
locations <- locations %>% slice(-1)
#Do a tibblings
locations <- as_tibble(locations)
#Change data types from character to double and integer
locations$COORD_LON %<>% as.double
locations$COORD_LAT %<>% as.double
locations$TOTAL_COUNT %<>% as.integer
#Total number of birds per location
#per_location <- locations %>%
# group_by(PLACE, COORD_LAT, COORD_LON) %>%
#summarise(sum = sum(TOTAL_COUNT)) %>%
#arrange(desc(sum))
#Total number of birds if Psalm is stupid
per_location <- locations %>%
group_by(PLACE) %>%
mutate(across(starts_with("COORD_"), last)) %>%
ungroup()
per_location <- per_location %>%
group_by(PLACE, COORD_LAT, COORD_LON) %>%
summarise(sum = sum(TOTAL_COUNT)) %>%
arrange(desc(sum))
#Remove text in brackets
per_location$PLACE <- gsub("\\s*\\[[^\\)]+\\]","",as.character(per_location$PLACE))
#Total number of birds by species per location
per_location_by_species <- locations %>%
group_by(NAME_SPECIES, PLACE) %>%
summarise(sum = sum(TOTAL_COUNT)) %>%
arrange(desc(sum))
#HYPERTEST
per_location_by_species <- locations %>%
group_by(NAME_SPECIES, PLACE) %>%
summarise(sum = sum(TOTAL_COUNT)) %>%
arrange(desc(sum))
#Bar plot
ggplot(per_location_by_species, aes(sum, NAME_SPECIES, fill = PLACE)) +
geom_bar(stat="identity", position = "dodge") +
labs(title="Number of birds per location")
print(per_location_by_species)
# Create mapview map
locations_sf <- st_as_sf(per_location, coords = c("COORD_LON", "COORD_LAT"), crs = 4326)
mapview(locations_sf, zcol="sum")
# Create Google Maps map
ggmap(stauteich_3) +
geom_point(data = per_location,
aes(x = COORD_LON, y = COORD_LAT, size=sum),
color = "red", alpha = 0.5) +
geom_text_repel(data = per_location, aes(x = COORD_LON, y = COORD_LAT, label = PLACE)) +
guides(color = guide_legend(override.aes = list(size = 1))) +
scale_size_continuous(range = c(1, 15))
print(per_location_by_species) %>% print(n=40)

65
birding_unique.R Normal file
View File

@ -0,0 +1,65 @@
library("ggmap")
library(tidyverse)
library(sf)
library(mapview)
library(dplyr)
library(ggrepel)
library(grid)
library(tidyverse)
library(shadowtext)
#Working directory
setwd("/Users/mjo-air/Programming/R")
#Colours
BLUE <- "#076fa2"
#API key for Google Maps
register_google(key = "")
#Map of Bielefeld, centered on Stauteich 3
stauteich_3 <- get_googlemap("bielefeld stauteich III", zoom = 14, maptype = "satellite")
#The variable used to get the sum of all birds that were seen at a particular location
#all_birds_sum_var <- read.csv("birding.csv", stringsAsFactors = F, na.strings="`")[,c('Location.1', 'Longitude', 'Latitude', 'Number', 'Common')]
#The variable used to get the sum of unique birds that were seen at a particular location
unique_birds_sum_var <- read.csv("birding.csv", stringsAsFactors = F, na.strings="`")[,c('Location.1', 'Longitude', 'Latitude', 'Common')]
#Convert to tibble
locations <- as_tibble(unique_birds_sum_var)
#Find how many unique sightings there are in this location
per_location_unique <- unique(locations)
#Do some grouping
per_location <- per_location_unique %>%
group_by(Location.1, Longitude, Latitude) %>%
summarise(sum = n()) %>%
arrange(desc(sum))
#Show table
print(per_location)
#Do some converterinos
locations_sf <- st_as_sf(per_location, coords = c("Longitude", "Latitude"), crs = 4326)
#Show as HTML map
mapview(locations_sf, zcol="sum")
#Create barplot
barplot(height=per_location$sum, names=per_location$Location.1, las=2)
#Create hyperplot
ggplot(per_location) +
geom_col(aes(sum, Location.1), fill = BLUE, width = 0.6)
#Google Maps map
ggmap(bw_map) +
geom_point(data = per_location,
aes(x = Longitude, y = Latitude, size=sum),
color = "red", alpha = 0.5) +
geom_text_repel(data = per_location, aes(x = Longitude, y = Latitude, label = Location.1)) +
guides(color = guide_legend(override.aes = list(size = 1))) +
scale_size_continuous(range = c(1, 15))