Quantatitive (Q) Variable: Histogram, density plot
Categorical (C) Variable: Bar plot
Bivariate
Q + Q: Scatterplot (each Q needs an axis)
Q + C: Multiple densities or histograms, multiple boxplots or violin plots
C + C: Stacked bar plot, proportional bar plot, side by side bar plot (or mosaic plots)
Multivariate
Use facets or aesthetics to incorporate more variables to bivariate plot.
Use heat maps or star plots to explore many variables quickly (try different ways of specifying x and y position).
Learning Goals
Plot data points on top of a map using the ggmap() function along with ggplot2 functions
Create choropleth maps using geom_map()
Add points and other ggplot2 features to a map created from geom_map()
Understand the basics of creating a map using leaflet, including adding points and choropleths to a base map
Template File
Download a template .Rmd of this activity. Put the file in a Assignment_04 folder within your COMP_STAT_112 folder.
This .Rmd contains examples that we’ll work on in class and 2 multi-part coding exercises you’ll finish for Assignment 4.
Spatial Viz
Points (Longitude, Latitude) on a Map
Contour/Heat Maps (2D Density) on a Map
Choropleths (color-filled polygons/regions)
Dynamic Maps
Points on a Map: Starbucks Example
The Starbucks data, compiled by Danny Kaplan, contains information about every Starbucks in the world at the time the data were collected.
# Starbucks locationsStarbucks <-read_csv("https://bcheggeseth.github.io/112_fall_2023/data/starbucks.csv")#In groups: What does each line of code do?ggplot(data = Starbucks) +geom_point(aes(x = Longitude, y = Latitude),alpha =0.2,size =0.2 ) +theme_classic()
Map Background + Points
# Get the map background information (images)world <-get_stamenmap(bbox =c(left =-180, bottom =-57, right =179, top =82.1),maptype ="terrain",zoom =2)# In groups: What does each line of code do?ggmap(world) +# Action: Creates the map "background"geom_point( # Action:data = Starbucks, # Action: aes(x = Longitude, y = Latitude), # Action:alpha =0.3, # Action:size =0.2# Action: ) +theme_map() # Action:
Error in (function (msg) : TopologyException: Input geom 0 is invalid: Self-intersection at -163.87651444908798 -84.347832316374607
Contour/Heat Maps
US_map2 <-get_stamenmap(bbox =c(left =-132, bottom =20, right =-65, top =55),maptype ="toner-lite",zoom =5)# Contour plotggmap(US_map2) +#Action: Set background mapgeom_density_2d( #Action: Set glyph to be a 2d density contourdata = Starbucks, #Action: Pass Starbucks data to geom_density_2daes(x = Longitude, y = Latitude), #Action: Set aesthetic mapping of x and y to data variables size =0.3) +#Action: Change line widththeme_map() #Action: Remove axes and labels
# 2D Density plotggmap(US_map2) +#Action: Set background mapgeom_density_2d_filled( #Action: Set glyph to be contours + bandsdata = Starbucks, #Action: Pass Starbucks data to geom_density_2d_filled aes(x = Longitude, y = Latitude), #Action: Set aesthetic mapping of x and y to data variables size =0.05, #Action: Change line width alpha = .5, #Action: Change transparency of fillbins =20, #Action: Change the number of binscolor ='white'#Action: Change of lines ) +scale_fill_viridis_d( #Action: Change the scale for fill color to follow viridis palette & remove guideguide ='none' ) +theme_map() #Action: Remove axes and labels
Choropleths
starbucks_us_by_state <- Starbucks %>%# Action: Save new object filter(Country =="US") %>%# Action: Keep rows in the UScount(`State/Province`) %>%# Action: Summarize rows by counting rows per statemutate(state_name =str_to_lower(abbr2state(`State/Province`))) # Action: Create new variable, state_name, as lower case state names using the state abbreviations# US states map information - coordinates used to draw bordersstates_map <-map_data("state")# map that colors state by number of Starbucksstarbucks_us_by_state %>%# Action: pass new dataset to ggplotggplot() +# Action: Create framegeom_map( # Action: Set glyph to be polygons from a reference mapmap = states_map, # Action: data of map coordinatesaes( # Action: links between aesthetics and variablesmap_id = state_name, # Action: set the variable to connect info in starbucks_us_by_state with map datasetfill = n # Action: set fill to represent the count ) ) +# This assures the map looks decently nice:expand_limits(x = states_map$long, y = states_map$lat) +theme_map()