-
Notifications
You must be signed in to change notification settings - Fork 507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
help creating choropleth map? #169
Comments
The first example @ https://rstudio.github.io/leaflet/shapes.html shows you how to do this. ?leaflet leads to: But you are sending class(mapStates) = "map" to leaflet. Maybe there's a way to convert it but I don't see why when you can find anything in maps in a more standard format. FWIW, you probably should just use data.frame instead of structure. In that example, after making sure your states$count lines up with the data used in that example, you would just replace states$AWATER with states$count. |
I am able to follow the example and create a map of the US (I only had to change 2013 for 2014)
What I am not sure is how to change the data to use
Thanks a lot @cholcomb ! |
Try using |
@ignacio82 this has in fact nothing to do with # Use data.table or dplyr as you prefer
library(data.table)
states <- readOGR("./cb_2014_us_state_500k", "cb_2014_us_state_500k")
# Make a copy of the SPDF attribute table, and then work normally, as with any data.frame/data.table object
states.dt <- data.table(states@data)
# Create an explicit attribute to keep polygons IDs (useful to "re-attach" the table to the polygons later)
states.dt[, rn := row.names(states)]
my_counts <- data.table(
State = c(
"CA", "TX", "IL", "FL", "NY", "OH",
"NJ", "GA", "MI", "PA", "MA", "CO", "AZ", "NC", "VA", "WA", "IN",
"MD", "MN", "WI", "MO", "TN", "IA", "KY", "LA", "SC", "CT", "AL",
"KS", "OR", "OK", "AR", "NV", "UT", "NE", "ID", "MS", "DC", "NM",
"NH", "ME", "AK", "RI", "MT", "HI", "WV", "SD", "ND", "DE", "VT",
"WY", "PR", "GU", "VI", "MP", "AS", "na", "MH", "FM", "PW"
),
count = c(
1590533L, 1016328L, 754535L, 742603L, 714205L,
538719L, 477278L, 452064L, 437162L, 428616L, 420332L, 391084L,
380853L, 354601L, 342533L, 335505L, 294670L, 286026L, 273427L,
246172L, 238968L, 236037L, 235030L, 209514L, 199013L, 191707L,
185521L, 179931L, 163477L, 159862L, 142610L, 136006L, 120111L,
117338L, 112671L, 106176L, 102564L, 100168L, 97496L, 69881L,
69508L, 68684L, 65631L, 62109L, 61123L, 57300L, 57254L, 56091L,
51696L, 33944L, 32136L, 4822L, 598L, 468L, 49L, 19L, 17L,
11L, 2L, 1L
)
)
# Index the 2 data.tables on their common keys and join them
setkey(states.dt, STUSPS)
setkey(my_counts, State)
states.dt <- my_counts[states.dt]
# Re-attach the attribute table to the SPDF
setkey(states.dt, rn)
states@data <- data.frame(states.dt[row.names(states)])
# Make sure polygons IDs and data.frame row.names match
states <- spChFIDs(states, states$rn)
# Create map as usual... |
Thanks a lot! |
My data looks like this:
I would like to create a map of the US using count to color each state. I can create a map of the US with this code:
but, I'm not sure how to change that code to tell it to use my data to color the states.
Thanks for the help!
The text was updated successfully, but these errors were encountered: