DA 101, Dr. Ladd
Week 8
Degrees, Minutes, Seconds format:
110°W 21’ 13”
Decimal Degrees format:
110.353611
(+/- for N/S or W/E)
You can always use an online conversion tool.
You need a “bounding box” to define the edges of your map.
Find bbox coordinates online at bboxfinder.com.
Plotting points in space is only the first step.
Let’s make this map together:
For the US, we can enter the bounding coordinates manually:
We do this using ggmap’s special get_stamenmap()
function.
Stamen is a map/geocoding service.
What happens if you lower the zoom value? What happens if you add a maptype to the get_stamenmap()
function?
Possible maptypes: “terrain”, “terrain-background”, “terrain-labels”, “terrain-lines”, “toner”, “toner-2010”, “toner-2011”, “toner-background”, “toner-hybrid”, “toner-labels”, “toner-lines”, “toner-lite”, “watercolor”
ggmap
comes with a crime
dataset we can use.
# Let's filter the data to only violent crimes
violent_crimes <- filter(crime, offense != "auto theft", offense != "theft", offense != "burglary")
# rank violent crimes
violent_crimes$offense <- factor(violent_crimes$offense,
levels = c("robbery", "aggravated
assault", "rape", "murder"))
# restrict to downtown
violent_crimes <- filter(violent_crimes,
-95.39681 <= lon & lon <= -95.34188,
29.73631 <= lat & lat <= 29.78400)
Wow! Try it with and without the darken parameter. What changes?
ggplot
.# Filter to only robberies
robberies <- violent_crimes %>% filter(offense == "robbery")
# Use density geometries
ggmap(map, extent="device") +
geom_density2d(data=robberies, aes(x=lon, y=lat), size=0.3) +
stat_density2d(data=robberies, aes(x=lon, y=lat, fill=..level.., alpha=..level..), size=0.01, bins=16, geom="polygon") +
scale_fill_gradient(low = "blue", high = "red") +
scale_alpha(range = c(0, 0.3), guide = "none")
Remember how the Garlic Mustard dataset had latitude and longitude variables? One thing we might need to know, in order to validate our study, is if there is geographic bias in where garlic mustard plots were.
Now do the same thing for Europe! How would you get the right bounding box?
Make a map of Denison University, and label a building or location on campus that you enjoy, with text that relates to why it’s special to you - or why you’re thankful for it.
geom_polygon()
.Challenge: Make a similar map for the urban population of US states.
Here’s some code to get you started:
# Get US states polygons
states <- map_data(map="state")
# Lowercase crime data
USArrests$region = tolower(rownames(USArrests))
What do you need to do next? Think about joining data before you try ggplot!