Monday, February 17, 2020

Cartogram for multimorbidity in England

#map-mM.R
#prevalence preserving map of multimorbidity in England
#GT 12 Jan 2016, 28 July 2017, 13 Dec 2019

#install.packages('Rcartogram', repos='http://www.omegahat.org/R', type='source')
#actually: stackoverflow.com/questions/31613119/installing-rcartogram-packages-error-message
#Use Geoff Lee's fork of Rcartogram github

# note carefully: PATH, fftw3-3, 
# downloading from github and unzipping the bundle, 
# setting working directory etc

#Do not use R package fftw!

#install.packages('devtools')
#  bits and pieces may need to be installed in piecemeal
#library(devtools)
#install_github('chrisbrunsdon/getcartr',subdir='getcartr')
library(Rcartogram)
library(getcartr)
library(rgdal)
library(ggplot2)
library(ggmap)
library(mapproj)
library(Cairo)
library(foreign)
library(scales)
library(dplyr)
library(plyr)

setwd('~/_projects/MALAM')
dir()
dir('England_lad_shp_2011')

#choro
engY <- read.dta('w7ticsLA.dta') #OSLAUA2 is chr
engY$OSLAUA2 <- as.factor(engY$OSLAUA2)
engY <- rename(engY, c("tics27demen"="Dementia"))
names(engY)
summary(engY$Dementia)  #min 0, max 19%
summary(engY$multimor)  #min 0, max 29%

engshp <- readOGR('England_lad_shp_2011/england_lad_2011_gen_clipped.shp')
names(engshp) #altname name code: all factors 
engshp <- fortify(engshp, region="code")
  # code -> id
eng2 <- merge(engshp, engY, by.x="id", by.y="OSLAUA2")

#Choro dementia
chorodem <- ggplot() + 
  geom_polygon(data = eng2, aes(long, lat, group=group,
    fill=Dementia)) +
  scale_fill_distiller(palette = "Reds", trans = "reverse", 
    labels = percent, breaks = pretty_breaks(n=5)) +
  guides(fill = guide_legend(reverse = TRUE)) +  
  theme_nothing(legend = TRUE)
chorodem
ggsave(chorodem, file = "dementiachoro.png", width = 5.5,
  height = 7, type = "cairo-png")

#Choro multimorbidity
choromm <- ggplot() + 
  geom_polygon(data = eng2, aes(long, lat, group=group,
    fill=multimor)) +
  scale_fill_distiller(palette = "Greens", trans = "reverse",
    labels = percent, breaks = pretty_breaks(n=5)) +
  guides(fill = guide_legend(reverse = TRUE)) +  
  theme_nothing(legend = TRUE)
choromm
ggsave(choromm, file = "multimorchoro.png", width = 5.5,
  height = 7, type = "cairo-png")

#carto dementia
engY <- read.dta('w7ticsLA.dta') #OSLAUA2 is chr
engY$OSLAUA2 <- as.factor(engY$OSLAUA2)
engY <- rename(engY, c("tics27demen"="Dementia"))
names(engY)

engshp <- readOGR('England_lad_shp_2011/england_lad_2011_gen_clipped.shp') #reread
match.indices <- match(engshp@data[, "code"], engY[, "OSLAUA2"])
engshp@data <- data.frame(engshp@data, engY[match.indices, ])

engshp.carto <- quick.carto(engshp, engshp@data$Dementia, blur=0)
  # minutes later...
engshp.buffer <- gBuffer(engshp.carto, width=0, byid=TRUE)
engshp.f <- fortify(engshp.buffer, region="OSLAUA2")
  #ONS_LABEL -> id
engshp.f <- merge(engshp.f, engshp@data, by.x="id", by.y="OSLAUA2")    #merge carto with map

#england.f$qimd <- england.f$qimd/100
cartodem <- ggplot() + 
  geom_polygon(data = engshp.f, aes(long, lat, group=group,
    fill=Dementia)) +
  scale_fill_distiller(palette = "Reds", trans = "reverse",
    labels = percent, breaks = pretty_breaks(n=5)) +
  guides(fill = guide_legend(reverse = TRUE)) +
  theme_nothing(legend = TRUE)  
cartodem
ggsave(cartodem, file = "dementiacarto.png", width = 5.5,
  height = 7, type = "cairo-png")

#carto multimor
engshp.cartomm <- quick.carto(engshp, engshp@data$multimor, blur=0)    # minutes later...
engshp.buffermm <- gBuffer(engshp.cartomm, width=0, byid=TRUE)
engshp.fmm <- fortify(engshp.buffermm, region="OSLAUA2")
  #ONS_LABEL -> id
engshp.fmm <- merge(engshp.fmm, engshp@data, by.x="id", by.y="OSLAUA2") #merge carto with map

cartomm <- ggplot() + 
  geom_polygon(data = engshp.f, aes(long, lat, group=group,
    fill=multimor)) +
  scale_fill_distiller(palette = "Greens", trans = "reverse",
    labels = percent, breaks = pretty_breaks(n=5)) +
  guides(fill = guide_legend(reverse = TRUE)) +
  theme_nothing(legend = TRUE)  
cartomm
ggsave(cartomm, file = "multimorcarto.png", width = 5.5,
  height = 7, type = "cairo-png")

0 Comments:

Post a Comment

<< Home