In this exercize, I will be creating several maps using the ggmap library in R, as well as building on skills I have learned in ggplot2 and Dplyr. The first map will be simple, and will involve creating a map of recorded Joshua Tree locations from ecological surveys in California.
devtools::install_github("dkahle/ggmap", ref = "tidyup")
## Skipping install of 'ggmap' from a github remote, the SHA1 (4dfe5165) has not changed since last install.
## Use `force = TRUE` to force installation
library(ggmap)
## Loading required package: ggplot2
## Google Maps API Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it: see citation("ggmap") for details.
register_google(key = "AIzaSyAI80it1h-3ml3hb_nlgyjp_9PQmjhNA_4",
account_type = "standard")
DesertMap <- get_map(location = c(-116.042,35.868),zoom=7)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=35.868,-116.042&zoom=7&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx-3ml3hb_nlgyjp_9PQmjhNA_4
Joshuatree<-read.csv("C:/Users/bcole/Documents/Y.brevifola.csv")
ggmap(DesertMap)+geom_point(aes(longitude,latitude), data=Joshuatree, color="green4")+ggtitle("Joshua Tree Location Map") +theme(plot.title = element_text(hjust = 0.5))
Creating this map proved more difficult than I had anticipated, as it required a google API key and different version of the GGMap library than the one I installed. Through a lot of online searching, I was able to come up with a code that was able to create the necessary map.
This map, while useful in describing locations, is unfortunately somewhat uninteresting, as the data only includes location information on the plants. In a future exercize, I might be able to expand upon this information.
For the next exercise, I wanted to create a visually appealing map of California counties. This map was useful, as it got me thinking about methods I could use in regards to polygon maps in R.
register_google(key = "AIzaSyAI80it1h-3ml3hb_nlgyjp_9PQmjhNA_4",
account_type = "standard")
counties<-map_data("county")
CA_counties<- subset(counties, region == 'california')
Californiamap <- get_map(location = c(-118.042,36.868),zoom=6, maptype = "roadmap")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=36.868,-118.042&zoom=6&size=640x640&scale=2&maptype=roadmap&language=en-EN&key=xxx-3ml3hb_nlgyjp_9PQmjhNA_4
calmap2<-ggmap(Californiamap)+geom_polygon(data=CA_counties, aes(x=long, y=lat, group=group, fill=subregion), color="blue")+ggtitle("California County Map") +theme(plot.title = element_text(hjust = 0.5))+theme(legend.key.size = unit(0.5,"line"))+labs(fill='County')
calmap2
This map seemed to work out well, as it was a great way of visually expressing the size and shape of counties in California.
For the next exercise, I combined the existing county map with data from the 2016 presidential election. This map would be able to demonstrate the value of having both point and polygon information.
register_google(key = "AIzaSyAI80it1h-3ml3hb_nlgyjp_9PQmjhNA_4",
account_type = "standard")
Election<-read.csv("C:/Users/bcole/Documents/2016election.csv")
Californiamap <- get_map(location = c(-118.042,36.868),zoom=6, maptype = "terrain")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=36.868,-118.042&zoom=6&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx-3ml3hb_nlgyjp_9PQmjhNA_4
calmap2+geom_point(aes(Longitude,Latitude, size=votes), data=Election, color="grey10", shape=20)+ggtitle("Votes by County in 2016 General Election") +theme(plot.title = element_text(hjust = 0.5))+scale_size_continuous(range = c(2, 8))+labs(size='Presidential Votes')
This maps shows the total number of votes for president in each county during the 2016 presidential election, with size adjusted for number of votes. Also included in the background is the county map created during the previous exercise. During future work, I hope to combine the county map and the election map into one dataset, similar to those used in official election maps.