### You will need to install the following R packages:
# install packages if needed (only need to do this once!)
install.packages(c("rgbif", "ggplot2", "tidyverse", "gganimate", "ggthemes", "sf", "tools", "maps", "gifski", "rnaturalearth", "rnaturalearthdata"))
################################################################
### Step 1: Copy and paste in this block ###################
################################################################
# load libraries
library(shiny)
library(ggplot2)
library(tidyverse)
library(gganimate)
library(ggthemes)
library(sf)
library("tools")
library(maps)
library("rnaturalearth")
library("rnaturalearthdata")
library(knitr)
library(rgbif)
## This bit can be modified and used to manually enter any species of interest, but skip for now
## enter the species you wish to analyze
## my_species <- readline(prompt="Enter your species: ")
### STOP ############################################################
#########################################################################
### Step 2: Copy and paste in this block ############################
#########################################################################
## can change basisOfRecord and limit to include observarions, etc.
## WARNING: This may take a few minutes, especially if there are lots and lots of records!
species <- occ_search(scientificName = "Lonicera japonica", basisOfRecord = "PRESERVED_SPECIMEN", country = 'US', limit = 30000)
mylat<-species$data$decimalLatitude
mylon<-species$data$decimalLongitude
myyear<-species$data$year
#create data frame of year, lat, and lon
mydf<-data.frame(myyear,mylat,mylon)
# omit rows with missing data (NA)
mydf<-na.omit(mydf)
# remove duplicate records based on latitude
mydf <- mydf %>% distinct(mylat, .keep_all = TRUE)
# create the animation for Latitude
p2 <- ggplot(mydf, aes(x = myyear, y = mylat, frame = myyear, cumulative = TRUE)) +
geom_point(shape = 21, colour="black", aes(fill="black"), size=5, stroke=1) +
## make the plot over time CUMULATIVE with shadow_mark
transition_states(myyear, transition_length = 1, state_length = 2) +
shadow_mark() +
## set the data limits; this will vary on year and geographic range
scale_x_continuous(limits=c(1800,2020)) +
scale_y_continuous(limits=c(20,55)) +
theme_minimal() +
xlab("Year") +
ylab("Latitude") +
theme(text=element_text(size=16, family="Georgia")) +
transition_time(myyear) +
labs(title = 'Latitude of Lonicera japonica records', subtitle = 'Year: {round(frame_time,0)}')
p2
#save animation as a .gif
anim_save("My_latitude.gif", p2)
###################################################################################
world <- ne_countries(scale = "medium", returnclass = "sf")
## class(world)
states <- st_as_sf(map("state", plot = FALSE, fill = TRUE))
## head(states)
states <- cbind(states, st_coordinates(st_centroid(states)))
#Generate the map and coordinate boundaries
usa <- ggplot(data = world) +
geom_sf(data = states, fill = NA) +
## change these to capture invasive range
coord_sf(xlim = c(-125, -65), ylim = c(20, 55), expand = FALSE)
# map the occurrence data
MyMap <- usa +
geom_point(data = mydf, shape = 21, colour="black", fill = "red", aes(x=mylon, y=mylat), size=4, stroke=1) +
#transition_states(myyear, transition_length = 1, state_length = 1) +
transition_time(myyear) +
labs(title = 'Documented locations of Lonicera japonica', subtitle = 'Year: {round(frame_time,0)}') +
shadow_mark()
#animate!
MyMap
#save animation as a .gif
anim_save("My_map.gif", JSmap)
# install packages if needed
install.packages(c("rgbif", "ggplot2", "tidyverse", "ggthemes", "sf", "tools", "maps", "rnaturalearth", "rnaturalearthdata", "rworldmap"))
## load libraries
library(rgbif)
library(ggplot2)
library(tidyverse)
library(ggthemes)
library(sf)
library(tools)
library(maps)
library(rnaturalearth)
library(rnaturalearthdata)
library(rworldmap)
# Get occurrence data
species <- occ_search(scientificName = "Lonicera japonica", basisOfRecord = "PRESERVED_SPECIMEN", country = 'US', limit = 30000)
# Define Lat, Lon, and Year
mylat<-species$data$decimalLatitude
mylon<-species$data$decimalLongitude
myyear<-species$data$year
# Create data frame of year, lat, and lon
mydf<-data.frame(myyear,mylat,mylon)
# Omit rows with missing data (NA)
mydf<-na.omit(mydf)
# Remove duplicate records based on latitude
mydf <- mydf %>% distinct(mylat, .keep_all = TRUE)
# Set up time slices
slice.1935 <- subset(awn, myyear <= 1880)
awns.1935 <- as.factor(slice.1935$awn)
slice.1950 <- subset(awn, myyear <= 1910)
awns.1950 <- as.factor(slice.1950$awn)
slice.1965 <- subset(awn, myyear <= 1940)
awns.1965 <- as.factor(slice.1965$awn)
slice.1980 <- subset(awn, myyear <= 1970)
awns.1980 <- as.factor(slice.1980$awn)
slice.1995 <- subset(awn, myyear <= 2000)
awns.1995 <- as.factor(slice.1995$awn)
slice.2020 <- subset(awn, myyear <= 2020)
awns.2020 <- as.factor(slice.2020$awn)
# Create the base map
world <- ne_countries(scale = "medium", returnclass = "sf")
## class(world)
states <- st_as_sf(map("state", plot = FALSE, fill = TRUE))
## head(states)
states <- cbind(states, st_coordinates(st_centroid(states)))
# Generate the map and coordinate boundaries
usa <- ggplot(data = world) +
geom_sf(data = states, fill = NA) +
## change these to capture invasive range
coord_sf(xlim = c(-125, -65), ylim = c(20, 55), expand = FALSE)
# map the occurrence data for 1935
JSmap.1935 <- usa +
geom_point(data = slice.1935, shape = 20, colour="red", aes(x=mylon, y=mylat), size=3, stroke=1) +
#transition_states(year, transition_length = 1, state_length = 1) +
#scale_fill_manual(values=c("red", "blue")) +
labs(title = '1880')
# map the occurrence data for 1950
JSmap.1950 <- usa +
geom_point(data = slice.1950, shape = 20, colour="darkorange", aes(x=mylon, y=mylat), size=3, stroke=1) +
#transition_states(year, transition_length = 1, state_length = 1) +
#scale_fill_manual(values=c("red", "blue"))+
labs(title = '1910')
# map the occurrence data for 1965
JSmap.1965 <- usa +
geom_point(data = slice.1965, shape = 20, colour="orange", aes(x=mylon, y=mylat), size=3, stroke=1) +
#transition_states(year, transition_length = 1, state_length = 1) +
#scale_fill_manual(values=c("red", "blue"))+
labs(title = '1940')
# map the occurrence data for 1980
JSmap.1980 <- usa +
geom_point(data = slice.1980, shape = 20, colour="green", aes(x=mylon, y=mylat), size=3, stroke=1) +
#transition_states(year, transition_length = 1, state_length = 1) +
#scale_fill_manual(values=c("red", "blue"))+
labs(title = '1970')
# map the occurrence data for 1995
JSmap.1995 <- usa +
geom_point(data = slice.1995, shape = 20, colour="blue", aes(x=mylon, y=mylat), size=3, stroke=1) +
#transition_states(year, transition_length = 1, state_length = 1) +
#scale_fill_manual(values=c("red", "blue"))+
labs(title = '2000')
# map the occurrence data for 2020
JSmap.2020 <- usa +
geom_point(data = slice.2020, shape = 20, colour="purple", aes(x=mylon, y=mylat), size=3, stroke=1) +
#transition_states(year, transition_length = 1, state_length = 1) +
#scale_fill_manual(values=c("red", "blue"))+
labs(title = '2020')