Geospatial

Continuous Outcome

  • {ggmap} Dot map

    • Example: Does Price vary by location?

    • In your data, find the min and max latitude and longitude to specify a bounding box

      library(ggmap)
      bbox <- c(left = min_longitude, 
                bottom = min_latitude, 
                right = max_longitude, 
                top = max_latitude)
      map_tiles <- get_stamenmap(bbox, zoom = 13)
    • (Optional) Aggregate some of the data (i.e. dots)

      agg_dat <- dat %>%
          group_by(latitude = round(latitude, 2),
                  longitude = round(longitude, 2)) %>%
          summarize(avg_outcome = mean(numeric_outcome),
                    n = n())
    • scale_size_continuous adjusts the range of dot sizes. This range makes them a little smaller.

      ggmap(map_tiles) +
          geom_point(aes(longitude, latitude, 
                         size = n, 
                         color = avg_outcome), 
                     data = agg_dat) +
          scale_color_gradient2(low = "blue", 
                                high = "red", 
                                midpoint = midpoint_value_of_numeric_outcome,
                                trans = "log10", 
                                labels = dollar) +
          scale_size_continuous(range = c(0.5, 4)) +
          theme_map() +
          labs(color = "avg_outcome", 
               size = "n")
      • trans and labels are for the legend (I think).