Maps– TX
### https://cran.r-project.org/web/packages/arcos/vignettes/county-analysis.html
library(arcos)
library(knitr)
library(tigris)
## To enable
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
##
## Attaching package: 'tigris'
## The following object is masked from 'package:graphics':
##
## plot
## Loading required package: viridisLite
## Registered S3 methods overwritten by 'readr':
## method from
## format.col_spec vroom
## print.col_spec vroom
## print.collector vroom
## print.date_names vroom
## print.locale vroom
## str.col_spec vroom
## ── Attaching packages ──────────────────────── tidyverse 1.2.1 ──
## âś” ggplot2 3.2.1.9000 âś” purrr 0.3.2
## âś” tibble 2.1.3 âś” dplyr 0.8.1
## âś” tidyr 0.8.3 âś” stringr 1.4.0
## âś” readr 1.3.1 âś” forcats 0.4.0
## ── Conflicts ─────────────────────────── tidyverse_conflicts() ──
## âś– dplyr::filter() masks stats::filter()
## âś– dplyr::lag() masks stats::lag()
##
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
##
## discard
## The following object is masked from 'package:readr':
##
## col_factor
## The following object is masked from 'package:viridis':
##
## viridis_pal
buyer_state | buyer_county | buyer_dea_no | buyer_name | buyer_city | total_dosage_unit | total_records |
---|---|---|---|---|---|---|
TX | BRAZOS | BW7145495 | WALGREEN CO. | BRYAN | 4701140 | 6510 |
TX | BRAZOS | BS2419566 | SCOTT & WHITE PHARMACY-B/CS | COLLEGE STATION | 2796040 | 4617 |
TX | BRAZOS | BW5571319 | WAL-MART PHARMACY 10-0322 | BRYAN | 2052980 | 7278 |
TX | BRAZOS | BW8168026 | WALGREEN CO. | COLLEGE STATION | 1468400 | 3432 |
TX | BRAZOS | BC5350210 | CVS PHARMACY, INC. | BRYAN | 1308600 | 1523 |
TX | BRAZOS | BW9510353 | WALGREEN CO. | BRYAN | 1207830 | 2143 |
ggplot(mingo,
aes(x=total_dosage_unit, y=fct_reorder(buyer_name, total_dosage_unit))) +
geom_segment(
aes(x = 0,
y=fct_reorder(buyer_name, total_dosage_unit),
xend = total_dosage_unit,
yend = fct_reorder(buyer_name, total_dosage_unit)),
color = "gray50") +
geom_point() +
scale_x_continuous(label=comma) +
labs(x="Dosage units", y="",
title = "Pills sold at Mingo, WV pharmacies",
subtitle = "Between 2006 and 2012",
caption = "Source: The Washington Post, ARCOS") +
theme_minimal()
BUYER_COUNTY | BUYER_STATE | year | count | DOSAGE_UNIT | countyfips |
---|---|---|---|---|---|
ANDERSON | TX | 2006 | 7027 | 2209130 | 48001 |
ANDERSON | TX | 2007 | 6006 | 2148570 | 48001 |
ANDERSON | TX | 2008 | 5937 | 2296470 | 48001 |
ANDERSON | TX | 2009 | 5864 | 2348990 | 48001 |
ANDERSON | TX | 2010 | 6123 | 2445130 | 48001 |
ANDERSON | TX | 2011 | 6715 | 2740100 | 48001 |
## Set the option for shapefiles to load with sf
options(tigris_class = "sf")
## Function to download county shapefiles in West Virginia
wv_shape <- counties(state="TX", cb=T)
##
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|== | 4%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|==== | 7%
|
|===== | 7%
|
|===== | 8%
|
|====== | 9%
|
|====== | 10%
|
|======= | 10%
|
|======= | 11%
|
|======== | 12%
|
|======== | 13%
|
|========= | 13%
|
|========= | 14%
|
|========= | 15%
|
|========== | 15%
|
|========== | 16%
|
|=========== | 16%
|
|=========== | 17%
|
|=========== | 18%
|
|============ | 18%
|
|============ | 19%
|
|============= | 19%
|
|============= | 20%
|
|============= | 21%
|
|============== | 21%
|
|============== | 22%
|
|=============== | 22%
|
|=============== | 23%
|
|=============== | 24%
|
|================ | 24%
|
|================ | 25%
|
|================= | 25%
|
|================= | 26%
|
|================= | 27%
|
|================== | 27%
|
|================== | 28%
|
|=================== | 29%
|
|=================== | 30%
|
|==================== | 30%
|
|==================== | 31%
|
|==================== | 32%
|
|===================== | 32%
|
|===================== | 33%
|
|====================== | 33%
|
|====================== | 34%
|
|====================== | 35%
|
|======================= | 35%
|
|======================= | 36%
|
|======================== | 36%
|
|======================== | 37%
|
|======================== | 38%
|
|========================= | 38%
|
|========================= | 39%
|
|========================== | 39%
|
|========================== | 40%
|
|========================== | 41%
|
|=========================== | 41%
|
|=========================== | 42%
|
|============================ | 42%
|
|============================ | 43%
|
|============================ | 44%
|
|============================= | 44%
|
|============================= | 45%
|
|============================== | 45%
|
|============================== | 46%
|
|============================== | 47%
|
|=============================== | 47%
|
|=============================== | 48%
|
|================================ | 49%
|
|================================ | 50%
|
|================================= | 50%
|
|================================= | 51%
|
|================================== | 52%
|
|================================== | 53%
|
|=================================== | 53%
|
|=================================== | 54%
|
|=================================== | 55%
|
|==================================== | 55%
|
|==================================== | 56%
|
|===================================== | 56%
|
|===================================== | 57%
|
|===================================== | 58%
|
|====================================== | 58%
|
|====================================== | 59%
|
|======================================= | 59%
|
|======================================= | 60%
|
|======================================= | 61%
|
|======================================== | 61%
|
|======================================== | 62%
|
|========================================= | 62%
|
|========================================= | 63%
|
|========================================= | 64%
|
|========================================== | 64%
|
|========================================== | 65%
|
|=========================================== | 65%
|
|=========================================== | 66%
|
|=========================================== | 67%
|
|============================================ | 67%
|
|============================================ | 68%
|
|============================================= | 68%
|
|============================================= | 69%
|
|============================================= | 70%
|
|============================================== | 70%
|
|============================================== | 71%
|
|============================================== | 72%
|
|=============================================== | 72%
|
|=============================================== | 73%
|
|================================================ | 73%
|
|================================================ | 74%
|
|================================================ | 75%
|
|================================================= | 75%
|
|================================================= | 76%
|
|================================================== | 76%
|
|================================================== | 77%
|
|================================================== | 78%
|
|=================================================== | 78%
|
|=================================================== | 79%
|
|==================================================== | 79%
|
|==================================================== | 80%
|
|==================================================== | 81%
|
|===================================================== | 81%
|
|===================================================== | 82%
|
|====================================================== | 82%
|
|====================================================== | 83%
|
|====================================================== | 84%
|
|======================================================= | 84%
|
|======================================================= | 85%
|
|======================================================== | 86%
|
|======================================================== | 87%
|
|========================================================= | 87%
|
|========================================================= | 88%
|
|========================================================== | 89%
|
|========================================================== | 90%
|
|=========================================================== | 90%
|
|=========================================================== | 91%
|
|============================================================ | 92%
|
|============================================================ | 93%
|
|============================================================= | 93%
|
|============================================================= | 94%
|
|============================================================== | 95%
|
|============================================================== | 96%
|
|=============================================================== | 96%
|
|=============================================================== | 97%
|
|=============================================================== | 98%
|
|================================================================ | 98%
|
|================================================================ | 99%
|
|=================================================================| 99%
|
|=================================================================| 100%
## Join the county dosage data we pulled
wv <- left_join(wv, wv_shape, by=c("countyfips"="GEOID"))
# Mapping with ggplot2, sf, and viridis
wv %>%
ggplot(aes(geometry=geometry, fill = DOSAGE_UNIT, color = DOSAGE_UNIT)) +
facet_wrap(~year, ncol=2) +
geom_sf() +
coord_sf(crs = 26915) +
scale_fill_viridis(direction=-1, label = comma) +
scale_color_viridis(direction=-1, label = comma) +
theme_void() +
theme(panel.grid.major = element_line(colour = 'transparent')) +
labs(title="Oxycodone and hydrocodone pills in Texas", caption="Source: The
Washington Post, ARCOS")
population <- county_population(state="TX", key="WaPo") %>%
# isolate the columns so it doesn't conflict in a join (there are doubles, that's why)
select(countyfips, year, population)
left_join(wv, population) %>%
mutate(per_person=DOSAGE_UNIT/population) %>%
ggplot(aes(geometry=geometry, fill = per_person, color = per_person)) +
facet_wrap(~year, ncol=2) +
geom_sf() +
coord_sf(crs = 26915) +
scale_fill_viridis(direction=-1, label = comma) +
scale_color_viridis(direction=-1, label = comma) +
theme_void() +
theme(panel.grid.major = element_line(colour = 'transparent')) +
labs(title="Oxycodone and hydrocodone pills in West Virginia per person", caption="Source: The Washington Post, ARCOS")
## Joining, by = c("year", "countyfips")