1 . Plot the raster:
ggmap(myMap)
3. Add polygons from shp file
The shp file is imported into R using the rgdal package, and must be transformed to geographic
coordinates (latitude/longitude) on the World Geodetic System of 1984 (WGS84) datum using
the rgdal package:
library(rgdal)
shpData <- readOGR(dsn="C:\\Documents and Settings\\Watershed", layer="WS")
proj4string(shpData) # describes data’s current coordinate reference system
# to change to correct projection:
shpData <- spTransform(shpData,
CRS("+proj=longlat +datum=WGS84"))
To plot the data:
geom_polygon(aes(x = long, y = lat, group=id),
data = shpData, color ="white", fill ="orangered4",
alpha = .4, size = .2)
Part 2: Plotting the maps and data
2 . Add points with latitude/longitude coordinates:
ggmap(myMap)+
geom_point(aes(x = Longitude, y = Latitude), data = data,
alpha = .5, color="darkred", size = 3)
alpha = transparency
color = color
size = size of points
The size, color, alpha, and shape of the points can be scaled relative
to another variable (in this case estArea) within the aes function:
ggmap(myMap)+
geom_point(aes(x = Longitude, y = Latitude, size=sqrt(estArea)),
data = data, alpha = .5, color="darkred")
Additional functions can be added to control scaling, e.g.:
ggmap(myMap)+
geom_point(aes(x = Longitude, y = Latitude, size=sqrt(estArea)),
data = data, alpha = .5, color="darkred")+
scale_size(range=c(3,20))
4. Annotate figure
baylor <- get_map('baylor university', zoom = 15, maptype = 'satellite')
ggmap(baylor) +
annotate('rect', xmin=-97.11, ymin=31.54, xmax=-97.12, ymax=31.55, col="red“, fill=“white”)+
annotate('text', x=-97.12, y=31.54, label = 'Statistical Sciences', colour = I(‘red'), size = 8)+
annotate('segment', x=-97.12, xend=-97.12, y=31.55, yend=31.55,
colour=I('red'), arrow = arrow(length=unit(0.3,"cm")), size = 1.5) +
labs(x = 'Longitude', y = 'Latitude') + ggtitle('Baylor University')
size
color
Controlling size and color
scale_size(range = c(3, 20))
But, the following is better for display because
it is based on area (rather than radius)
scale_area(range=c(3,20))
Continuous variables: color gradient between
n colors, e.g.:
scale_colour_gradientn(colours =
rainbow_hcl(7))
Discrete variables, e.g.:
scale_colour_manual(values=rainbow_hcl(7))
scale_colour_manual(values=c("8" = "red",
"4" = "blue","6" = "green“)
*Use colorspace and RColorBrewer to choose
color combinations
color= outline color
fill = polygon color
alpha = transparency
size = outline size
ggmap quickstart
Page 2 ggmap,
Melanie Frazier