Calochortus Species Distribution Map

This code will allow you to create a species distribution map spanning three regions - Canada, Mexico, and the US.
Author

Julianna J. Harden
& Annelisa N. Martinez

Published

April 24, 2024

1 Introduction to Calochortus

Calochortus that is commonly found throughout the Pacific Coast of North America and widely distributed in California.

Calochortus venustus Morphotypes

2 Set your working directory to the folder you want your output to be located

setwd()

3 Install and load necessary packages

#install.packages("dplyr")
library("dplyr")
#install.packages('raster')
library(raster) # For raster creation and manipulation
#install.packages('ggplot2')
library(ggplot2)
#install.packages('maptools')
library(maptools)
#install.packages('ggspatial')
library(ggspatial)
#install.packages('sf')
library(sf)
#install.packages('ggnewscale')
library(ggnewscale)
#install.packages('plotly')
library(plotly)
#install.packages("tidyverse")
library(tidyverse)
#install.packages("viridis")
library(viridis)
#install.packages("hrbrthemes")
library(hrbrthemes)
#install.packages("mapdata")
library(mapdata)
#install.packages("rgdal")
library(rgdal) # For spatial data manipulation
#install.packages("sp")
library(sp)     # For spatial data handling

4 Calochortus Species Distribution

Calo<-subset(calochortus_occ,calochortus_occ$genus=='Calochortus')
Calo$genus<-as.factor(Calo$genus)
colnames(Calo)[1]<-'Calochortus'

5 Obtain state boundary data for the regions using the function “map_data” in ggplot2

states_usa <- map_data("state")
states_mexico <- map_data("world", region = "Mexico")
states_canada <- map_data("world", region = "Canada")

6 Create the plot using ggplot2

Calochort <- ggplot() +
  geom_map(data = states_usa, map = states_usa,
           aes(x = long, y = lat, map_id = region),
           color = 'black', fill = "azure", size = 0.3) +  # Add state borders for the USA
  geom_map(data = states_mexico, map = states_mexico,
           aes(x = long, y = lat, map_id = region),
           color = 'black', fill = "honeydew", size = 0.3) +  # Add country borders for Mexico
  geom_map(data = states_canada, map = states_canada,
           aes(x = long, y = lat, map_id = region),
           color = 'black', fill = "lavender", size = 0.3) +  # Add country borders for Canada
  scale_fill_gradient(name = 'Elevation (m)', low = 'white', high = 'black') +
  coord_sf(xlim = c(-125, -90), ylim = c(12, 50)) +
  geom_point(mapping = aes(x = as.numeric(decimalLongitude), 
                           y = as.numeric(decimalLatitude),
                           colour = Calochortus,
                           fill = Calochortus,
                           shape = Calochortus),
             data = Calo, cex = 0.25) +
  scale_shape_manual(name = 'Calochortus', values = c(22, 21, 24, 23)) +
  scale_color_manual(values = c('grey26', '#000000', '#000000', '#000000')) +
  scale_fill_manual(values = c('grey76', '#1E88E5', '#FF880A', '#8C0808')) +
  theme_bw() +
  theme(panel.grid.major = element_blank(),  # Remove major gridlines
        panel.grid.minor = element_blank())  # Remove minor gridlines

7 This will create a PDF file of your distribution map

pdf(file = "4Apr24calo_distribution.pdf",   # The directory you want to save the file in
   width = 8, # The width of the plot in inches
  height = 8) # The height of the plot in inches
Calochort
dev.off()

7.1 Use Plotly to Create an Interactive Map

REMINDER: If you choose to visualize your map using plotly, make sure to comment out the scale fill gradient line in the second to last step.

p=ggplotly(Calochort)  
htmlwidgets::saveWidget(as_widget(p), "Calochortus.html")

8 Final Product

Calochortus Species Distribution