install.packages(“tidyverse”) install.packages(“devtools”) devtools::install_github(“kjhealy/socviz”) install.packages(“viridis”) install.packages(“plotly”)

library(tidyverse)
## ── Attaching packages ─────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.0     ✔ purrr   0.2.5
## ✔ tibble  1.4.2     ✔ dplyr   0.7.7
## ✔ tidyr   0.8.2     ✔ stringr 1.3.1
## ✔ readr   1.1.1     ✔ forcats 0.3.0
## ── Conflicts ────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(socviz)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(viridis)
## Loading required package: viridisLite

Create and glimpse a data frame of map data

Use the map_data function with the name of the map you want to use, “state”. Other options include arguments such as “county”,“usa”,“france”,“italy”,“world”, and “world2”

library(maps)
## 
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
## 
##     map
us_states <- map_data("state")
glimpse(us_states)
## Observations: 15,537
## Variables: 6
## $ long      <dbl> -87.46201, -87.48493, -87.52503, -87.53076, -87.5708...
## $ lat       <dbl> 30.38968, 30.37249, 30.37249, 30.33239, 30.32665, 30...
## $ group     <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1...
## $ order     <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1...
## $ region    <chr> "alabama", "alabama", "alabama", "alabama", "alabama...
## $ subregion <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...

Glimpse the opiates data

glimpse(opiates)
## Observations: 800
## Variables: 11
## $ year          <int> 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, ...
## $ state         <chr> "Alabama", "Alaska", "Arizona", "Arkansas", "Cal...
## $ fips          <int> 1, 2, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 17,...
## $ deaths        <int> 37, 27, 229, 28, 1474, 164, 151, 32, 28, 402, 83...
## $ population    <int> 4430141, 624779, 5023823, 2651860, 33499204, 422...
## $ crude         <dbl> 0.8, 4.3, 4.6, 1.1, 4.4, 3.9, 4.5, 4.1, 4.9, 2.6...
## $ adjusted      <dbl> 0.8, 4.0, 4.7, 1.1, 4.5, 3.7, 4.4, 4.1, 4.9, 2.6...
## $ adjusted_se   <dbl> 0.1, 0.8, 0.3, 0.2, 0.1, 0.3, 0.4, 0.7, 0.9, 0.1...
## $ region        <ord> South, West, West, South, West, West, Northeast,...
## $ abbr          <chr> "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", ...
## $ division_name <chr> "East South Central", "Pacific", "Mountain", "We...

Options for preserving original data

save(opiates, file = “opiates.Rdata”) load(“opiates.Rdata”)

# Option: save to external file
save(opiates, file = "opiates.Rdata")
load("opiates.Rdata")

# Option: create copy of dataset in environment
opiates_orig <- opiates

# Option: create copy of column in dataset
opiates <- opiates %>% mutate(region_orig = region)
glimpse(opiates)
## Observations: 800
## Variables: 12
## $ year          <int> 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, ...
## $ state         <chr> "Alabama", "Alaska", "Arizona", "Arkansas", "Cal...
## $ fips          <int> 1, 2, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 17,...
## $ deaths        <int> 37, 27, 229, 28, 1474, 164, 151, 32, 28, 402, 83...
## $ population    <int> 4430141, 624779, 5023823, 2651860, 33499204, 422...
## $ crude         <dbl> 0.8, 4.3, 4.6, 1.1, 4.4, 3.9, 4.5, 4.1, 4.9, 2.6...
## $ adjusted      <dbl> 0.8, 4.0, 4.7, 1.1, 4.5, 3.7, 4.4, 4.1, 4.9, 2.6...
## $ adjusted_se   <dbl> 0.1, 0.8, 0.3, 0.2, 0.1, 0.3, 0.4, 0.7, 0.9, 0.1...
## $ region        <ord> South, West, West, South, West, West, Northeast,...
## $ abbr          <chr> "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", ...
## $ division_name <chr> "East South Central", "Pacific", "Mountain", "We...
## $ region_orig   <ord> South, West, West, South, West, West, Northeast,...

Prepare to join datasets: opiates, us_states

opiates$region <-tolower(opiates$state)
glimpse(opiates)
## Observations: 800
## Variables: 12
## $ year          <int> 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, ...
## $ state         <chr> "Alabama", "Alaska", "Arizona", "Arkansas", "Cal...
## $ fips          <int> 1, 2, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 17,...
## $ deaths        <int> 37, 27, 229, 28, 1474, 164, 151, 32, 28, 402, 83...
## $ population    <int> 4430141, 624779, 5023823, 2651860, 33499204, 422...
## $ crude         <dbl> 0.8, 4.3, 4.6, 1.1, 4.4, 3.9, 4.5, 4.1, 4.9, 2.6...
## $ adjusted      <dbl> 0.8, 4.0, 4.7, 1.1, 4.5, 3.7, 4.4, 4.1, 4.9, 2.6...
## $ adjusted_se   <dbl> 0.1, 0.8, 0.3, 0.2, 0.1, 0.3, 0.4, 0.7, 0.9, 0.1...
## $ region        <chr> "alabama", "alaska", "arizona", "arkansas", "cal...
## $ abbr          <chr> "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", ...
## $ division_name <chr> "East South Central", "Pacific", "Mountain", "We...
## $ region_orig   <ord> South, West, West, South, West, West, Northeast,...
opiates_map <- left_join(us_states,opiates)
## Joining, by = "region"
glimpse(opiates_map)
## Observations: 246,781
## Variables: 17
## $ long          <dbl> -87.46201, -87.46201, -87.46201, -87.46201, -87....
## $ lat           <dbl> 30.38968, 30.38968, 30.38968, 30.38968, 30.38968...
## $ group         <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
## $ order         <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
## $ region        <chr> "alabama", "alabama", "alabama", "alabama", "ala...
## $ subregion     <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...
## $ year          <int> 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, ...
## $ state         <chr> "Alabama", "Alabama", "Alabama", "Alabama", "Ala...
## $ fips          <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
## $ deaths        <int> 37, 43, 57, 71, 49, 83, 80, 124, 165, 185, 206, ...
## $ population    <int> 4430141, 4447100, 4467634, 4480089, 4503491, 453...
## $ crude         <dbl> 0.8, 1.0, 1.3, 1.6, 1.1, 1.8, 1.8, 2.7, 3.5, 3.9...
## $ adjusted      <dbl> 0.8, 1.0, 1.3, 1.6, 1.1, 1.8, 1.8, 2.7, 3.6, 4.1...
## $ adjusted_se   <dbl> 0.1, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3...
## $ abbr          <chr> "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", ...
## $ division_name <chr> "East South Central", "East South Central", "Eas...
## $ region_orig   <ord> South, South, South, South, South, South, South,...

##Create the theme_map function

theme_map <- function(base_size=9, base_family="") {
    require(grid)
    theme_bw(base_size=base_size, base_family=base_family) %+replace%
        theme(axis.line=element_blank(),
              axis.text=element_blank(),
              axis.ticks=element_blank(),
              axis.title=element_blank(),
              panel.background=element_blank(),
              panel.border=element_blank(),
              panel.grid=element_blank(),
              panel.spacing=unit(0, "lines"),
              plot.background=element_blank(),
              legend.justification = c(0,0),
              legend.position = c(0,0)
              )
}
p0 <- ggplot(data = subset(opiates_map, year > 1999),
             mapping = aes(x = long, y = lat,
                 group = group,
                 fill = adjusted))

p1 <- p0 + geom_polygon(color = "gray90", size = 0.05) +
    coord_map(projection = "albers", lat0 = 39, lat1 = 45) 

p2 <- p1 + scale_fill_viridis_c(option = "plasma")

p2 + theme_map() + facet_wrap(~ year, ncol = 3) +
    theme(legend.position = "bottom",
          strip.background = element_blank()) +
    labs(fill = "Death rate per 100,000 population ",
         title = "Opiate Related Deaths by State, 2000-2014")
## Loading required package: grid