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)