Note that there are some explanatory texts on larger screens.

plurals
  1. POCounties are scrambled in R
    primarykey
    data
    text
    <p>I'm using <code>ggplot2</code> to create a population density choropleth. It's currently working for single states, but not for multiples. It appears that the densities of various counties (that often have the same name) get mixed up, and sometimes even non-name matching counties are mixed up between states. For example, "New Jersey" gives the correct densities, but "New Jersey", "New York" tells me that the very populous Essex County in NJ has a density &lt;30p/mi^2. Why is this? </p> <pre><code>library(stringr) library(ggplot2) library(scales) library(maps) popdensitymap &lt;- function(...){ path &lt;- "U:/maps-county2011.csv" states &lt;- list(...) countydata &lt;- read.csv(path, sep=",") countydata &lt;- data.frame(countydata$X, countydata$Population.Density) names(countydata) &lt;- c("fips", "density") data(county.fips) cdata &lt;- countydata cdata$fips &lt;- gsub("^0", "", cdata$fips) countyinfo &lt;- merge(cdata, county.fips, by.x="fips", by.y="fips") countyinfo &lt;- data.frame(countyinfo, str_split_fixed(countyinfo$polyname, ",", 2)) names(countyinfo) &lt;- c('fips', 'density', 'polyname', 'state', 'county') countyshapes &lt;- map_data("county", states) countyshapes &lt;- merge(countyshapes, countyinfo, by.x="subregion", by.y="county") choropleth &lt;- countyshapes choropleth &lt;- choropleth[order(choropleth$order), ] choropleth$density_d &lt;- cut(choropleth$density, breaks=c(0,30,100,300,500,1000,3000,5000,100000)) state_df &lt;- map_data("state", states) density_d &lt;- choropleth$density_d choropleth &lt;- choropleth[choropleth$state %in% tolower(states),] p &lt;- ggplot(choropleth, aes(long, lat, group=group)) p &lt;- p + geom_polygon(aes(fill=density_d), colour=alpha("white", 1/2), size=0.2) p &lt;- p + geom_polygon(data = state_df, colour="black", fill = NA) p &lt;- p + scale_fill_brewer(palette="PuRd") p } </code></pre> <p>To use,</p> <pre><code>popdensitymap("New Jersey") popdensitymap("New York", "New Jersey") </code></pre> <p><a href="http://www.census.gov/popest/data/maps/2011/maps-county2011.csv" rel="nofollow noreferrer">Here is the csv.</a> It is very ugly, but I do not have access to a file sharing system right now. </p> <p>Here is an example of the output. As you can see, the extremely populous Essex County by New York City is inaccurately represented. <img src="https://i.stack.imgur.com/OGAKz.png" alt="enter image description here"></p> <p>EDIT: <a href="https://www.dropbox.com/s/jx2c93a7gn3ksr4/maps-county2011.csv" rel="nofollow noreferrer">Here is my version of the CSV.</a> Sorry for the dropbox delay. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload