What is a Map?

A bunch of latitude longitude points…

Plot of states

From the maps and ggplot2 package we can load the coordinates of each state, and create an accurate depiction of the United States.

library(maps)
library(ggplot2)

states <- map_data("state") # Pulls state data from ggplot2

qplot(long, lat, geom="point", data=states,
      main = "Plot of USA with Points") # plot points of lat and long coordinants

In this qplot() command, we are plotting a bunch of latitude and longitude points. This is cool, but does not look too appealing.

What is a Map?

… that are connected with lines in a very specific order.

qplot(long, lat, geom="path", data=states,group=group, # group = group order to connect lines
      main = "Plot of USA with Connected Lines") + 
  coord_map() # Scales map

Now, we connect the lines in a certain way using the group=group parameter. Now we have a better looking map.

Basic Map Data

What needs to be in the data set in order to plot a basic map?

  • Need latitude/longitude points for all map boundaries
  • Need to know which boundary group all lat/long points belong
  • Need to know the order to connect points within each group

Data for Building Basic State Map

Our states data has all necessary information

head(states)
##        long      lat group order  region subregion
## 1 -87.46201 30.38968     1     1 alabama      <NA>
## 2 -87.48493 30.37249     1     2 alabama      <NA>
## 3 -87.52503 30.37249     1     3 alabama      <NA>
## 4 -87.53076 30.33239     1     4 alabama      <NA>
## 5 -87.57087 30.32665     1     5 alabama      <NA>
## 6 -87.58806 30.32665     1     6 alabama      <NA>

Categorical Information Using Hue

If a categorical variable is assigned as the all color then qplot() will assign different hues for each category.

Here we define the western and eastern conference in the NBA based on states.

western <- c("california","nevada","oregon","washington","idaho","montana","wyoming",
              "colorado","utah","north dakota","south dakota","nebraska",
             "kansas","missouri","iowa","minnesota","arizona","new mexico","texas",
             "oklahoma","tennessee","louisiana","arkansas","mississippi","alaska",
             "hawaii")
eastern <- c("wisconsin","michigan","illinois","indiana","ohio","wisconsin",
             "michigan","illinois","indiana","ohio","virginia","west virginia",
             "kentucky","north carolina","south carolina", "florida","georgia",
             "alabama","maine","new hampshire","vermont","massachusetts",
             "new york","rhode island","connecticut","new jersey","pennsylvania",
             "delaware","maryland")
# Create vector
State <- c(western,eastern) 
# Create data frame with labeling
StateGroups <- c( rep("Western Conference", length(western)),
              rep("Eastern Conference", length(eastern)))
# combine vectors into data frame
statereg <- data.frame(State, StateGroups) 
# Merge additional data into data frame
states.class.map <- merge(states, statereg, by.x="region", by.y="State", all.x=T) 
states.class.map <- states.class.map[order(states.class.map$order),]

The breakdown of code is as follows:

  • StateGroups: vector containing the strings "West Conference" and "Eastern Conference"
  • statereg: creating a data frame where the states are labeled into conferences based on StateGroups
  • states.class.map: merges all variables together to so we can plot each states and separate into conferences.

If further elaboration is need, one can simply print out each line.

Plot

We now create out plot where our data consists of the previously created data frame.

qplot(long, lat, geom="polygon", data=states.class.map, group=group,
      fill=StateGroups, colour=I("black"), # fill the map according the states group
      main = "Plot of USA by Conferences") + 
  coord_map() # coord_maps() scales image appropriately

Note that, there is no clear distinction in the separation of conferences. This meaning that Mississippi could easily be labeled to the Eastern Conference.

Cleaning Up Your Maps

Use ggplot2 options to clean up your map!

  • Adding Titles + ggtitle(…)
  • Might want a plain white background + theme_bw()
  • Extremely familiar geography may eliminate need for latitude and longitude axes + theme(…)
  • Keep aspect ratios correct + coord_map()

qplot(long, lat, geom="polygon", data=states.class.map, group=group,
      fill=StateGroups, colour=I("black")) + 
  coord_map()+
  ggtitle("Division of Eastern and Western Conference")+
  theme(axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.text.y = element_blank(),
axis.title.y = element_blank()) 

Your Turn

  1. Determine what each of the new parameters above changes.
  2. Change the map so that Mississippi is in the Eastern Conference and plot the map.

Answers

1.

  • theme(): starts environment to change parameters
  • axis.text.x = element_blank(): Removes \(x\)-axis tick marks
  • axis.text.y = element_blank(): Removes \(y\)-axis tick marks
  • axis.title.x = element_blank(): Removes \(x\)-axis title
  • axis.title.y = element_blank(): Removes \(y\)-axis title

2.

western <- c("california","nevada","oregon","washington","idaho","montana","wyoming",
              "colorado","utah","north dakota","south dakota","nebraska",
             "kansas","missouri","iowa","minnesota","arizona","new mexico","texas",
             "oklahoma","tennessee","louisiana","arkansas","alaska",
             "hawaii")

eastern <- c("wisconsin","michigan","illinois","indiana","ohio","wisconsin",
             "michigan","illinois","indiana","ohio","virginia","west virginia",
             "kentucky","north carolina","south carolina", "florida","georgia",
             "alabama","maine","new hampshire","vermont","massachusetts",
             "new york","rhode island","connecticut","new jersey","pennsylvania",
             "delaware","maryland","mississippi")

# Create vector
State <- c(western,eastern) 
# Create data frame with labeling
StateGroups <- c( rep("Western Conference", length(western)),
              rep("Eastern Conference", length(eastern)))
# combine vectors into data frame
statereg <- data.frame(State, StateGroups) 
# Merge additional data into data frame
states.class.map <- merge(states, statereg, by.x="region", by.y="State", all.x=T) 
states.class.map <- states.class.map[order(states.class.map$order),]

qplot(long, lat, geom="polygon", data=states.class.map, group=group,
      fill=StateGroups, colour=I("black"), # fill the map according the states group
      main = "Plot of USA by Conferences") + 
  coord_map() # coord_maps() scales plot appropriately

Labeling Specific Locations (More Advanced)

Using the package ggmap, we can plot specific locations. Using the command geocode(), we can produce the longitudinal and latitudinal coordinates of most landmarks.

Example

Using geocode() we can determine the coordinates for the University of Memphis, and using qmap() we can plot a google maps snippet.

library(ggmap)
geocode("University of Memphis")
##         lon      lat
## 1 -89.93714 35.11873

Plot

qmap("University of Memphis",zoom = 15)

We can also plot a bunch of universities on a map, as long as we make the location data frame first.

locationdf <- data.frame(rep(NA,5),rep(NA,5),rep(NA,5))

locationdf[1,2:3] <- geocode("University of Arizona") 
locationdf[1,1] <- "University of Arizona"

locationdf[2,2:3] <- geocode("University of Louisiana")
locationdf[2,1] <- "University of Louisiana"

locationdf[3,2:3] <- geocode("University of Connecticut")
locationdf[3,1] <- "University of Arizona"

locationdf[4,2:3] <- geocode("University of Florida")
locationdf[4,1] <- "University of Florida"

locationdf[5,2:3] <- geocode("University of Wisconsin")
locationdf[5,1] <- "University of Wisconsin"

colnames(locationdf) <- c("University","Longitude","Latitude")

Plot

p <- ggplot() #1
                    
p <- p + geom_polygon(data=states, aes(x=long, y=lat, group = group),colour = "white") #2
                    
p <- p + geom_point(data=locationdf, aes(x = Longitude,y = Latitude), color="blue" ) #3
                    
p <- p + geom_text(data=locationdf, hjust=0.5, vjust=-0.5,
aes(x=Longitude, y=Latitude, label=University), colour="coral1", size=3 ) #4

p <- p + coord_map() #5
p <- p + ggtitle("Maps of USA with University Locations") #6
p

Your Turn

  1. Determine what each of the layers of code above adds.

  2. Instead of universites, add the location of 5 sports team on the map of the USA. Any sport is fine. Change the color of the points and text.

Answers

1.

  • # 1: Creates an empty canvas
  • # 2: Creates map of USA with white lines separating states
  • # 3: Plots location of each university in blue
  • # 4: Adds text next to each point in coral color
  • # 5: Scales plot appropriately
  • # 6: Adds title

2.

locationdf <- data.frame(rep(NA,5),rep(NA,5),rep(NA,5))
locationdf[1,2:3] <- geocode("Green Bay") 
locationdf[1,1] <- "Green Bay Packers"
locationdf[2,2:3] <- geocode("Miami")
locationdf[2,1] <- "Miami Dolphins"
locationdf[3,2:3] <- geocode("Seattle")
locationdf[3,1] <- "Seattle Seahawks"
locationdf[4,2:3] <- geocode("Boston")
locationdf[4,1] <- "New England Patriots"
locationdf[5,2:3] <- geocode("Nashville")
locationdf[5,1] <- "Tennessee Titans"
colnames(locationdf) <- c("Team","Longitude","Latitude")

p <- ggplot() 
                    
p <- p + geom_polygon(data=states, aes(x=long, y=lat, group = group),colour = "white") 
                    
p <- p + geom_point(data=locationdf, aes(x = Longitude,y = Latitude), color="yellow" ) 
                    
p <- p + geom_text(data=locationdf, hjust=0.5, vjust=-0.5,
aes(x=Longitude, y=Latitude, label=Team), colour="green", size=3 ) 

p <- p + coord_map() #5
p <- p + ggtitle("Maps of USA with NFL Team Locations")
p