4

Can someone help me with creating a map in R? I need to make a map of the number of retail stores a company has in each state. All I need is the store count displayed in each state.

For example: if there are 150 stores in California and 250 in Texas, I need the map to display 150 within the California borders and 250 within the Texas borders.

I created a map with:

m = map("states", region = c('Alabama', etc....))

I tried adding the numbers as a label but that didn't work.

m = map("states", region = c('Alabama', etc....), labels = c('24','56',etc....))

I must be missing something. I have a .csv file with the state name and the number of stores in each state. Is there an easy way to translate this into a graph? I'd like to avoid doing it by hand in Excel.


Thanks! That worked. Does anyone know how to deal with the labels that do not fit within a states boarders? Also, does anyone know how to color code states based on the number value? The image of the map and code I am using is below: US Map With Counts library("maps")

map.text("state", regions=c("alabama", "arizona", "arkansas", "california", "colorado", "connecticut", "delaware", "district of columbia", "florida", "georgia", "idaho", "illinois", "indiana", "iowa", "kansas", "kentucky", "louisiana", "maine", "maryland", "massachusetts:main", "michigan:north", "minnesota", "mississippi", "missouri", "montana", "nebraska", "nevada", "new hampshire", "new jersey", "new mexico", "new york:main", "north carolina:main", "north dakota", "ohio", "oklahoma", "oregon", "pennsylvania", "rhode island", "south carolina", "south dakota", "tennessee", "texas", "utah", "vermont", "virginia:main", "washington:main", "west virginia", "wisconsin", "wyoming"), labels=as.character(c(29, 38, 13, 173, 21, 12, 5, 1, 108, 59, 7, 40, 46, 3, 9, 40, 23, 8, 24, 20, 49, 9, 14, 27, 2, 4, 12, 7, 21, 12, 60, 73, 2, 108, 18, 14, 71, 1, 35, 1, 49, 117, 10, 4, 39, 27, 18, 10, 2)))

whuber
  • 69,783
  • 15
  • 186
  • 281
Nathan Wadhwani
  • 71
  • 1
  • 1
  • 3

2 Answers2

3

Try the following:

library("maps")
map.text("state", regions=c("california", "texas"), labels=as.character(c(125, 250)))

R plot

rcs
  • 3,894
  • 1
  • 27
  • 30
0

There is an excellent blog that goes step by step into creating a choropleth map. Their example is specific to Europe, although you should have no trouble incorporating your own data. I particularly like how they incorporate the powerful graphics engine of ggplot2 in their workflow.

enter image description here

library(maptools)
library(ggplot2)
library(ggmap)

# read administrative boundaries (change folder appropriately)
eurMap <- readShapePoly(fn="NUTS_2010_60M_SH/Shape/data/NUTS_RG_60M_2010")

# read downloaded data (change folder appropriately)
eurEdu <- read.csv("educ_thexp_1_Data.csv", stringsAsFactors = F)
eurEdu$Value <- as.double(eurEdu$Value) #format as numeric

# merge map and data
eurEduMapDf <- merge(eurMapDf, eurEdu, by.x="id", by.y="GEO")
eurEduMapDf <- eurEduMapDf[order(eurEduMapDf$order),]

#limit data to main Europe
europe.limits <- geocode(c("Cape Fligely, Rudolf Island, Franz Josef Land, Russia", "Gavdos, Greece", "Faja Grande, Azores", "Severny Island, Novaya Zemlya, Russia"))

eurEduMapDf <- subset(eurEduMapDf, long > min(europe.limits$lon) & long < max(europe.limits$lon) & lat > min(europe.limits$lat) & lat < max(europe.limits$lat))

# ggplot mapping
# data layer
m0 <- ggplot(data=eurEduMapDf)
# empty map (only borders)
m1 <- m0 + geom_path(aes(x=long, y=lat, group=group), color='gray') + coord_equal()

# fill with education expenditure data
m2 <- m1 + geom_polygon(aes(x=long, y=lat, group=group, fill=Value))

# inverse order (to have visible borders)
m0 <- ggplot(data=eurEduMapDf)
m1 <- m0 + geom_polygon(aes(x=long, y=lat, group=group, fill=Value)) + coord_equal()
m2 <- m1 + geom_path(aes(x=long, y=lat, group=group), color='black')
m2

# over a GoogleMap (not working if not correctly projected)
map <- get_map(location = 'Europe', zoom=4)
m0 <- ggmap(map)
m1 <- m0 + geom_polygon(aes(x=long, y=lat, group=group, fill=Value), data=eurEduMapDf, alpha=.9)
m2 <- m1 + geom_path(aes(x=long, y=lat, group=group), data=eurEduMapDf, color='black')

# add text
library(doBy)
txtVal <- summaryBy(long + lat + Value ~ id, data=eurEduMapDf, FUN=mean, keep.names=T)
m3 <- m2 + geom_text(aes(x=long, y=lat, label=Value), data=txtVal, col="yellow", cex=3)
Aaron
  • 51,658
  • 28
  • 154
  • 317