Professional Documents
Culture Documents
Week 3 Spatial Data Code and Results
Week 3 Spatial Data Code and Results
Combine Datasets
Now we can combine our dataframe of counts of number of species per county, we can join these data back into
our county map data so that we can plot the county polygons. First we will convert our SpatialPolygon-class
US county map into a dataframe for joining and plotting. We will then use left_join to join our count
dataframe into our county maps dataframe, matching by the unique county id.
states.df <- fortify(usa, region = "ID")
sallys.counties <- left_join(states.df, counts.df, by='id')
head(sallys.counties)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
1
2
3
4
5
6
1
2
3
4
5
6
long
-86.50517
-86.53382
-86.54527
-86.55673
-86.57966
-86.59111
counts
22
22
22
22
22
22
50
Latitude (N)
45
40
Number of
Species
35
30
30
20
10
0
25
120
100
80
Longitude (W)
Filter Data
bama.rich <- sallys.counties[grepl('alabama,', sallys.counties$id),]
Define Map
Similarly to the US map, we will create a map of the number of salamander species in Alabama and store it.
Because The scale is much smaller, we will adjust the fill gradient. Just to make it look fancy, well add an
off-centered gray polygon to make it look like the plot has a shadow. To show a different x and y axes labels,
we adjusted the x and y scale pasting the N/W directly after the number.
bama.map <- ggplot() +
geom_polygon(data=bama.rich,
aes(x=long+0.05, y=lat-0.05, group=id), fill='gray80') +
geom_polygon(data=bama.rich,
aes(x=long, y=lat, group=id, fill=counts),
color="black", size=0.25) +
coord_equal() +
scale_fill_gradient2(name="Number of\nSpecies", low="white", high='darkblue',
midpoint=22, mid='steelblue1') +
theme_bw() +
scale_x_continuous(breaks=c(-88,-87,-86,-85),
labels=c(paste(c(88:85), 'W', sep=''))) +
scale_y_continuous(limits=c(29.8,35.25),
breaks=c(seq(30, 35, 1)),
labels=c(paste(seq(30, 35, 1), 'N', sep=''))) +
labs(x='', y='')
Define Inset
We will use the states.df to plot our inset map. To define our zoomed-in map of Alabama, by plotting the
state of Alabama with darker red lines.
states <- map("state", fill=TRUE, plot=FALSE)
states.df2 <- fortify(states, region='id')
inset.map <- ggplot() +
geom_polygon(data=states.df2, aes(long, lat, group=group),
color='black', fill='white', size=0.25) +
geom_polygon(data=states.df2[states.df2$region=='alabama',], aes(long, lat, group=group),
color='darkred', fill='white', size=1) +
coord_equal() +
theme_bw() +
labs(x=NULL, y=NULL) +
theme(axis.text.x=element_blank(), axis.text.y=element_blank(),
axis.ticks=element_blank(), axis.title.x=element_blank())
35N
34N
Number of
Species
33N
26
24
22
20
32N
18
31N
30N
88W
87W
86W
85W
Add Points
Lets say that we randomly sampled the state of Alabama, and we want to visualize the proportion of species
that we found at each site. We will use the dismo package to create the random sampling locations throughout
the state. To create a random sampling of points, we need to use a raster object as the mask. We will also
create a proportion species found column. Thus, when we add these data points to our previous plot we
can scale the symbol size to be proportional to the proportion of species we found at that site.
1
2
3
4
5
6
x
-87.59375
-86.30625
-87.18125
-85.68125
-85.80625
-87.88125
y prop.spp.found
31.81
0.6313152
33.85
0.7202442
33.99
0.7492431
31.15
0.9298679
34.01
0.7590958
34.35
0.5575507
10
11
35N
34N
Number of
Species
26
24
33N
22
20
18
Proportion of
Species Found
0.6
32N
0.7
0.8
0.9
31N
30N
88W
87W
86W
12
85W