Note that there are some explanatory texts on larger screens.

plurals
  1. POChoropleth mapping issue in R
    primarykey
    data
    text
    <p>EDIT: I have realized the source of my problem. I only have count information for the counties which I have data for, which is less than the number of counties in the area I'm plotting against.</p> <p>It stands to reason that the problem lines of code are here:</p> <pre><code>mapnames &lt;- map("county",plot=FALSE)[4]$names colorsmatched &lt;- d$colorBuckets [na.omit(match(mapnames ,d$stcon))] </code></pre> <p>Does anyone have advice on how to generate a vector of the appropriate length that would match the # of counties in NY, NJ, CT, and PA from the maps library? I want to merge the count data I have and include zeros for the counties I don't have information on.</p> <p>I am trying to follow the tutorial described here: <a href="http://www.thisisthegreenroom.com/2009/choropleths-in-r/" rel="nofollow noreferrer">http://www.thisisthegreenroom.com/2009/choropleths-in-r/</a></p> <p>The below code executes, but it is either not matching my dataset with the maps_counties data properly, or it isn't plotting it in the order I would expect. For example, the resulting areas for the greater NYC area show no density while random counties in PA show the highest density.</p> <p>The general format of my data table is:</p> <pre><code>county state count fairfield connecticut 17 hartford connecticut 6 litchfield connecticut 3 new haven connecticut 12 ... ... westchester new york 70 yates new york 1 luzerne pennsylvania 1 </code></pre> <p>Note this data is in order by state and then county and includes data for CT, NJ, NY, &amp; PA. </p> <p>First, I read in my data set:</p> <pre><code>library(maps) library(RColorBrewer) d &lt;- read.table("gissum.txt", sep="\t", header=TRUE) #Concatenate state and county info to match maps library d$stcon &lt;- paste(d$state, d$county, sep=",") #Color bins colors = brewer.pal(5, "PuBu") d$colorBuckets &lt;- as.factor(as.numeric(cut(d$count,c(0,10,20,30,40,50,300)))) </code></pre> <p>Here is my matching</p> <pre><code>mapnames &lt;- map("county",plot=FALSE)[4]$names colorsmatched &lt;- d$colorBuckets [na.omit(match(mapnames ,d$stcon))] </code></pre> <p>Plotting:</p> <pre><code>map("county" ,c("new york","new jersey", "connecticut", "pennsylvania") ,col = colors[d$colorBuckets[na.omit(match(mapnames ,d$stcon))]] ,fill = TRUE ,resolution = 0 ,lty = 0 ,lwd= 0.5 ) map("state" ,c("new york","new jersey", "connecticut", "pennsylvania") ,col = "black" ,fill=FALSE ,add=TRUE ,lty=1 ,lwd=2 ) map("county" ,c("new york","new jersey", "connecticut", "pennsylvania") ,col = "black" ,fill=FALSE ,add=TRUE , lty=1 , lwd=.5 ) title(main="Respondent Home ZIP Codes by County") </code></pre> <p>I am sure I am missing something basic re: the order in which the maps function plots items - but I can't seem to figure it out. Thanks for the help. Please let me know if you need any more information.</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