Objectives

Using the spatial visualization techniques, explore this data set on Pennsylvania hospitals (http://www.arcgis.com/home/item.html?id=eccee5dfe01e4c4283c9be0cfc596882). Create a series of 5 maps that highlight spatial differences in hospital service coverage for the state of PA.

To help you in getting the data imported into R, I have included the code below:

To import the data I use the foreign package, if you do not have it than be sure to install it prior to testing the code.

library(ggmap)
## Warning: package 'ggmap' was built under R version 3.5.2
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.5.2
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(leaflet)
library(ggplot2)
library(foreign)
HospitalsPA <- read.dbf("/Users/katie/R Data/ANLY 512 - Data Viz/pennsylv/pennsylv.dbf")  # change this to connect to your version
View(HospitalsPA)

The dataset contains a number of variables about each hospital, many of them are clear and straight forward.

names(HospitalsPA)
##   [1] "acc_trauma" "air_amb"    "als"        "arc_street" "arc_zone"  
##   [6] "bas_ls"     "bassinets"  "bb_id"      "bc_beds"    "bc_sus_bed"
##  [11] "beds_sus"   "birthing_r" "bone_marro" "burn_car"   "burn_care" 
##  [16] "card_beds"  "card_surge" "card_sus_b" "cardiac"    "cardiac_ca"
##  [21] "cardio_reh" "chemo"      "city"       "clin_lab"   "clin_psych"
##  [26] "county"     "countyname" "ct_scan"    "cty_key"    "cystoscopi"
##  [31] "deliv_rms"  "dental"     "detox_alc_" "diag_radio" "diag_xray" 
##  [36] "doh_hosp"   "doh_phone"  "emer_dept"  "endoscopie" "fac_id"    
##  [41] "facility"   "flu_old"    "fred_con_1" "fred_conta" "fred_email"
##  [46] "fred_fax"   "fred_hosp"  "fred_pager" "fred_phone" "gamma_knif"
##  [51] "gen_outpat" "gene_counc" "heart_tran" "helipad"    "hemodial_c"
##  [56] "hemodial_m" "hosp_id"    "hospice"    "hyper_cham" "icu"       
##  [61] "icu_beds"   "icu_sus_be" "inpat_flu_" "inpat_pneu" "kidney_tra"
##  [66] "labor_rms"  "lic_beds"   "lic_dent"   "lic_dos"    "lic_mds"   
##  [71] "lic_pod"    "linear_acc" "lithotrips" "liver_tran" "loc_method"
##  [76] "ltc"        "mcd"        "mcd_key"    "mcd_name"   "medical"   
##  [81] "mob_ccu"    "mob_icu"    "mri"        "ms1"        "neo2_beds" 
##  [86] "neo2_sus_b" "neo3_beds"  "neo3_sus_b" "neuro_surg" "neurology" 
##  [91] "obs_gyn"    "occ_ther"   "optometry"  "organ_bank" "ped_trauma"
##  [96] "pediatric"  "pet"        "pharmacy"   "phys_med"   "phys_ther" 
## [101] "podiatry"   "providerid" "psych"      "psych_inpa" "reg_trauma"
## [106] "resp_ther"  "so_flu_65u" "social_wor" "speech_pat" "street"    
## [111] "surgical"   "surgical_s" "thera_radi" "typ_org"    "typ_serv"  
## [116] "ultrasound" "x"          "y"          "zip"

Now create 5 maps, including descriptions, that highlight the spatial distribution of hosptical services in the state of PA. Upload these maps as a document to rpubs.com and submit that link the Moodle assignment.

#1. Hospitals in Chester County
HospitalsPA1<-subset(HospitalsPA, county == "Chester")
qmplot(x, y, data = HospitalsPA1, color = I('Red'), size = I(2), darken = .2, extent = "panel", main = "Hospitals in Chester County", xlab = "Longitude", ylab = "Latitude")
## Using zoom = 11...
## Source : http://tile.stamen.com/terrain/11/592/774.png
## Source : http://tile.stamen.com/terrain/11/593/774.png
## Source : http://tile.stamen.com/terrain/11/594/774.png
## Source : http://tile.stamen.com/terrain/11/592/775.png
## Source : http://tile.stamen.com/terrain/11/593/775.png
## Source : http://tile.stamen.com/terrain/11/594/775.png
## Source : http://tile.stamen.com/terrain/11/592/776.png
## Source : http://tile.stamen.com/terrain/11/593/776.png
## Source : http://tile.stamen.com/terrain/11/594/776.png

#2. Hospitals with X-Ray service in Chester County

register_google(key = "AIzaSyCepLjMb-v9bGHccTC2IgYHvO9DKeqrygU")
ChesterCounty <- geocode("chester, pennsylvania", source = "google")
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=chester,+pennsylvania&key=xxx-v9bGHccTC2IgYHvO9DKeqrygU
centerChesterCounty<-as.numeric(ChesterCounty)
ggmap(get_googlemap(center = centerChesterCounty, zoom=10, maptype = "terrain", color = 'color'))  +
  stat_density2d(aes(x, y, color = diag_xray), data = HospitalsPA1, bins = 5, main = "Hospitals in PA with Dental Services")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=39.849557,-75.355746&zoom=10&size=640x640&scale=2&maptype=terrain&key=xxx-v9bGHccTC2IgYHvO9DKeqrygU
## Warning: Ignoring unknown parameters: main
## Warning: Removed 2 rows containing non-finite values (stat_density2d).

#Requires enabling static map API 
#?get_googlemap
#3. Hospitals in Philaphia w/Helipad 

Phila <- subset(HospitalsPA, city=="Philadelphia")
ggmap(get_map(location = "Philadephia", zoom = "auto", maptype = "satellite")) + geom_point(aes(x,y, color = helipad), data = Phila)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=Philadephia&zoom=10&size=640x640&scale=2&maptype=satellite&language=en-EN&key=xxx-v9bGHccTC2IgYHvO9DKeqrygU
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Philadephia&key=xxx-v9bGHccTC2IgYHvO9DKeqrygU

#geom_point(aes(x, y, colour = liver_tran, size = lic_dent), data = dat)
#4. Hospitals in Philaphia w/Helipad and Liver Transplant

PhilaHelipad <-subset(Phila, helipad == "Y")
View(PhilaHelipad)

ggmap(get_map(location = "Philadephia", zoom = "auto", maptype = "roadmap")) + geom_point(aes(x,y, color = liver_tran), data = PhilaHelipad)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=Philadephia&zoom=10&size=640x640&scale=2&maptype=roadmap&language=en-EN&key=xxx-v9bGHccTC2IgYHvO9DKeqrygU
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Philadephia&key=xxx-v9bGHccTC2IgYHvO9DKeqrygU

#5. Hospitals in Philaphia w/Helipad and card beds

ggmap(get_map(location = "Philadephia", zoom = "auto", maptype = "roadmap")) + geom_point(aes(x,y, color = helipad, size = card_beds), data = HospitalsPA)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=Philadephia&zoom=10&size=640x640&scale=2&maptype=roadmap&language=en-EN&key=xxx-v9bGHccTC2IgYHvO9DKeqrygU
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Philadephia&key=xxx-v9bGHccTC2IgYHvO9DKeqrygU
## Warning: Removed 259 rows containing missing values (geom_point).