![]() When the correct data is attached to the shape object, a choropleth can be drawn with one line of code: qtm(US, fill = "PCT_OBESE_ADULTS10") US <- append_data(US, df, key.shp = "FIPS", key.data = "FIPS") # leave out AK, HI, and PR (state FIPS: 02, 15, and 72) # download shape (a little less detail than in the other scripts) Library(maptools) # for unionSpatialPolygonsĭownload.file("", destfile = "DataDownload.xls", mode="wb")ĭf <- read_excel("DataDownload.xls", sheet = "HEALTH") The reading and preprocessing of the data is similar to jlhoward's answer, with some differences: library(tmap) # package for plotting Maybe a little late for another answer, but still worthwhile to share I think. Gg <- gg + geom_map(data=obesity_map, map=us$map,Īes(map_id=FIPS, fill=obesity_map$obese),īut I always end up getting an error saying: "Error: Argument must be coercible to non-negative integer"Īny idea? Thanks again for all your help! I appreciate it so much. # add a new geom with our population (choropleth) Obesity_map$obese <- cut(obesity_map$obesity, ![]() I'm not entirely sure what I've done wrong: library(ggcounty) I also tried using ggcounty by following the example put up but I keep getting an error. It's a huge amount of numbers because it's for every US county so I abbreviated the results and put in the first couple lines.īasically, the data frame looks like this though: print(head(obesity_map)) Here's the output of the dput: dput(obesity_map) I'm fairly new to this so I'd appreciate any and all help! But what I really want is something like this below but with each county filled in: Ggplot(map.county, aes(x = long, y = lat, group=group, fill=as.factor(value))) + geom_polygon(colour = "white", size = 0.1)Īnd it basically creates an image that looks like this:Īs you can see, the US is divided into strange shapes, the colors aren't one consistent color in varying gradients, and you can't make much from it. Map.county <- merge(county.obesity, map.county, all=TRUE) Map.data <- map.dataĬounty.obesity <- ame(region = obesity_map$state, subregion = obesity_map$county, value = obesity_map$obesity_rate) Map.data <- merge(us.state.map, df, by='region', all=T) States <- levels(as.factor(us.state.map$region))ĭf <- ame(region = states, value = runif(length(states), min=0, max=100),stringsAsFactors = FALSE) I'm trying to visualize this on the map by showing various obesity rates per county throughout the US with this: us.state.map <- map_data('state') ![]() It looks more or less like this: obesity_map = ame(state, county, obesity_rate) So I have a data frame in R called obesity_map which basically gives me the state, county, and obesity rate per county. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |