Last active
November 25, 2020 04:10
-
-
Save jebyrnes/0030715178ba77264268 to your computer and use it in GitHub Desktop.
Basic methods for dealing with shapefiles for marine ecoregions of the world.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################################### | |
# Code to import and make useful | |
# Spalding et al. 2007's Marine Ecoregions of the World | |
# Data provided by http://www.marineregions.org/downloads.php | |
# by Jarrett Byrnes | |
# last updated 3/26/2015 | |
######################################################################### | |
###################### | |
##### Load Libraries | |
###################### | |
#for visualization | |
library(ggmap) | |
library(ggplot2) | |
library(RColorBrewer) | |
#for some data reduction | |
library(dplyr) | |
#for geospatial tools | |
library(rgdal) | |
library(maptools) | |
library(rgeos) | |
#Let's see what's in this shapefile! | |
ogrInfo("../../MEOW-TNC", "meow_ecos") | |
#get an ecoregions shapefile, and from that make a provience and realm shapefile | |
#from http://www.marineregions.org/downloads.php | |
#http://www.marineregions.org/sources.php#meow | |
regions <- readOGR("../../MEOW-TNC", "meow_ecos") | |
###### | |
#Make Provinces shapefile | |
###### | |
#Unite the spatial polygons for each region into one | |
provinces <- unionSpatialPolygons(regions, regions$PROVINCE) | |
#Make a data frame that will have Province level info and above | |
prov_data <- regions@data %>% | |
group_by(PROVINCE) %>% | |
summarise(PROV_CODE = PROV_CODE[1], REALM = REALM[1], RLM_CODE=RLM_CODE[1], Lat_Zone=Lat_Zone[1]) | |
#merge the polygons with the new data file | |
#note the row.names argument to make sure they map to each other | |
provinces <- SpatialPolygonsDataFrame(provinces, | |
data=data.frame(join(data.frame(PROVINCE=names(provinces)), | |
prov_data), | |
row.names=row.names(provinces))) | |
####### | |
#make realms shapefile | |
######## | |
#make spatial polygons for realms | |
realms <- unionSpatialPolygons(regions, regions$REALM) | |
#make new data | |
realm_data <- regions@data %>% | |
group_by(REALM) %>% | |
summarise(RLM_CODE = RLM_CODE[1], Lat_Zone=Lat_Zone[1]) | |
#merge the two! | |
realms <- SpatialPolygonsDataFrame(realms, | |
data=data.frame(join(data.frame(REALM=names(realms)), | |
realm_data), | |
row.names=row.names(realms))) | |
#########Plot them all | |
par(mfrow=c(2,2), mar=c(0,0,0,0)) | |
plot(regions, main="Ecoregion", cex.main=5) | |
plot(provinces, lwd=2, border="red", main="Province") | |
plot(realms, lwd=2, border="blue", main="Realm") | |
par(mfrow=c(1,1)) | |
########## | |
#OK - let's make some data frames | |
# so we can use ggplot2 and other tools | |
########## | |
#Turn SpatialPolygons into data frames | |
#from https://github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles | |
regions@data$id = rownames(regions@data) | |
regions.points = fortify(regions, ECOREGION="id") | |
regions.df = join(regions.points, regions@data, by="id") | |
#now do it for provinces | |
provinces@data$id = rownames(provinces@data) | |
provinces.points = fortify(provinces, PROVINCES="id") | |
provinces.df = join(provinces.points, provinces@data, by="id") | |
#####Make some ggplots for later visualization | |
base_ecoregion_ggplot <- ggplot(regions.df) + theme_bw() + | |
aes(long,lat,group=group) + | |
geom_polygon(fill=NA) + | |
geom_path(color="black") + | |
coord_equal() | |
base_province_ggplot <- ggplot(provinces.df) + theme_bw() + | |
aes(long,lat,group=group) + | |
geom_polygon(fill=NA) + | |
geom_path(color="black") + | |
coord_equal() | |
############# | |
# Show how filling works | |
############# | |
#let's make some fancy colors | |
library(RColorBrewer) | |
#Make a data frame with Ecoregion as an identifier | |
thing <- data.frame(ECOREGION = regions$ECOREGION, | |
score = runif(nrow(regions), -100, 100)) | |
#merge the score data with the regions data frame | |
regions.df2 <- merge(regions.df, thing) | |
#plot! | |
ggplot(regions.df2) + theme_bw() + | |
aes(long,lat,group=group) + | |
geom_polygon(mapping=aes(fill=score)) + | |
geom_path(color="black") + | |
coord_equal() + | |
scale_fill_gradientn(colours=brewer.pal(11, "Spectral")) | |
Hi. I have grouped certain ecoregions and I want to give specific colors to each group. Do you know how I can do it?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yup, I found it to with the same search terms!