Create a heat map of the United States with state abbreviations and a characteristic frequency in R

I would like to create a map of the USA (possibly a heat map) to show the frequency of a specific characteristic among states. I am not sure which package to use or my data is in the correct form. My data is in the tf table

tf AB AK AL AN AR AZ CA CO CT DC DE EN FL GA HI IA ID IL IN KS 1 21 31 1 12 56 316 53 31 16 7 1 335 63 11 42 29 73 40 2 

For the most part, my abbreviations are USA (except for a few Canadian copies). What is the best suggested approach for graphically displaying this on a map?

Now, how to get a granularity of less than 50 per color?

enter image description here

+8
dictionary r heatmap
source share
2 answers

two packages: cards, ggplot2. There is a great example:? Map_data ()

to start:

 tf= structure(list(state = structure(1:14, .Label = c("AK", "AL", "AR", "AZ", "CA", "CO", "CT", "DE", "FL", "GA", "IA", "IL", "IN", "KS"), class = "factor"), num = c(21L, 31L, 12L, 56L, 316L, 53L, 31L, 7L, 335L, 63L, 42L, 73L, 40L, 2L), region = structure(c(2L, 1L, 4L, 3L, 5L, 6L, 7L, 8L, 9L, 10L, 13L, 11L, 12L, 14L), .Label = c("alabama", "alaska", "arizona", "arkansas", "california", "colorado", "connecticut", "delaware", "florida", "georgia", "illinois", "indiana", "iowa", "kansas"), class = "factor")), .Names = c("state", "num", "region" ), class = "data.frame", row.names = c(NA, -14L)) require(maps);require(ggplot2) states <- map_data("state") tfmerged <- merge(states, tf, sort = FALSE, by = "region") tfmerged <- tfmerged[order(tfmerged$order), ] qplot(long, lat, data = tfmerged, group = group, fill = num, geom="polygon") 

Then fill in the remaining status information.

+7
source share

Another approach with spplot :

 library(maps) library(maptools) library(sp) 

Read the data first and add a column with state names:

 txt <- "AB AK AL AN AR AZ CA CO CT DC DE EN FL GA HI IA ID IL IN KS 1 21 31 1 12 56 316 53 31 16 7 1 335 63 11 42 29 73 40 2" dat <- stack(read.table(text = txt, header = TRUE)) names(dat)[2] <-'state.abb' dat$states <- tolower(state.name[match(dat$state.abb, state.abb)]) 

Then you get the map and convert it to SpatialPolygons :

 mapUSA <- map('state', fill = TRUE, plot = FALSE) nms <- sapply(strsplit(mapUSA$names, ':'), function(x)x[1]) USApolygons <- map2SpatialPolygons(mapUSA, IDs = nms, CRS('+proj=longlat')) 

And now you are adding information from your data:

 idx <- match(unique(nms), dat$states) dat2 <- data.frame(value = dat$value[idx], state = unique(nms)) row.names(dat2) <- unique(nms) USAsp <- SpatialPolygonsDataFrame(USApolygons, data = dat2) 

Finally, you build it:

 spplot(USAsp['value']) 

Image added enter image description here

+7
source share

All Articles