[R] choropleth packages (US)

Adrian Waddell adrian.waddell at gmail.com
Mon Dec 14 21:05:18 CET 2015


Alaska and Hawaii can be found in the 'world' or 'world2' databases of
the 'maps' package. The following is a bit a hack but it works

----

library(maps)
library(scales)


mergeMaps <- function(...) {
  maps <- list(...)
  if (length(maps) < 2)
    stop("need at least two maps")
  map <- maps[[1]]
  for (i in 2:length(maps)) {
    map$x <- c(map$x, NA, maps[[i]]$x)
    map$y <- c(map$y, NA, maps[[i]]$y)
    map$names <- c(map$names, maps[[i]]$names)
  }
  map$range <- c(range(map$x, na.rm = TRUE), range(map$y, na.rm = TRUE))
  map
}

shiftMap <- function(map, xmin=-180) {
  sel <- !is.na(map$x)
  map$x <- (map$x - xmin) %% 360 + xmin
  map$range <- c(range(map$x, na.rm = TRUE), range(map$y, na.rm = TRUE))
  map
}

m <- shiftMap(mergeMaps(map('state', fill=TRUE, plot=FALSE),
                        map('world', 'USA:Alaska', fill=TRUE, plot=FALSE),
                        map('world', 'Hawaii', fill=TRUE, plot=FALSE)),
              xmin=0)

s_data <- tolower(rownames(USArrests))
s_map <- tolower(m$names)

mapping <- lapply(s_data, function(state) {
  which(grepl(state, s_map))
})
## check if the mapping is good!

col_pal <- col_numeric("Greens", domain=NULL, na.color = 'lightyellow')

cols <- rep('lightyellow', length(s_data))

Map(function(indices, col) {
  cols[indices] <<- col
}, mapping, col_pal(USArrests$UrbanPop))

map(m, col=cols, fill=TRUE)
# map.axexs()

## or with no borders
map(m, col=cols, fill=TRUE, border=NA)

----

Greetings,

Adrian



On Fri, Dec 11, 2015 at 1:22 AM, Benjamin Tyner <btyner at gmail.com> wrote:
> Very nice Adrian. Is there a straightforward way to add Alaska and Hawaii at
> the lower left? (without resorting to choroplethr package)
>
>
> On 12/10/2015 06:09 AM, Adrian Waddell wrote:
>>
>> Hi,
>>
>> You can also use the 'maps' package for the map data and the 'scales'
>> package for the color mapping.
>>
>> E.g.
>>
>> library(maps)
>> library(scales)
>>
>> m <- map('state', fill=TRUE, plot=FALSE)
>>
>> s_data <- tolower(rownames(USArrests))
>> s_map <- tolower(m$names)
>>
>> mapping <- lapply(s_data, function(state) {
>>    which(grepl(state, s_map))
>> })
>> ## check if the mapping is good!
>>
>> col_pal <- col_numeric("Greens", domain=NULL, na.color = 'lightyellow')
>>
>> cols <- rep('lightyellow', length(s_data))
>>
>> Map(function(indices, col) {
>>    cols[indices] <<- col
>> }, mapping, col_pal(USArrests$UrbanPop))
>>
>> map(m, col=cols, fill=TRUE)
>>
>>
>> Adrian
>>
>>
>>
>> On Mon, Dec 7, 2015 at 9:34 AM, Erich Neuwirth
>> <erich.neuwirth at univie.ac.at> wrote:
>>>
>>> ggplot2 also can do this with
>>> fortify
>>> geom_polygon
>>>
>>> Von meinem iPad gesendet
>>>
>>>> Am 06.12.2015 um 21:03 schrieb Benjamin Tyner <btyner at gmail.com>:
>>>>
>>>> Hi
>>>>
>>>> I wish to draw a basic choropleth (US, by state) and am wondering if
>>>> anyone has any recommendations? I've tried the following thus far:
>>>>
>>>> 1. choroplethr: this works, but required installation of 30+
>>>> dependencies. I would prefer something with fewer dependencies.
>>>> 2. tmap: this also seems promising, but most of the examples I saw were
>>>> specific to European maps. Can it be adapted for US?
>>>> 3. statebins: doesn't draw true choropleths, but I liked that it doesn't
>>>> have many dependencies.
>>>>
>>>> Regards
>>>> Ben
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list