Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 737043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:40:30+00:00 2026-05-14T07:40:30+00:00

EDIT: I have realized the source of my problem. I only have count information

  • 0

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.

It stands to reason that the problem lines of code are here:

mapnames <- map("county",plot=FALSE)[4]$names
colorsmatched <- d$colorBuckets [na.omit(match(mapnames ,d$stcon))]

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.

I am trying to follow the tutorial described here: http://www.thisisthegreenroom.com/2009/choropleths-in-r/

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.

The general format of my data table is:

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

Note this data is in order by state and then county and includes data for CT, NJ, NY, & PA.

First, I read in my data set:

library(maps)
library(RColorBrewer)
d <- read.table("gissum.txt", sep="\t", header=TRUE)

#Concatenate state and county info to match maps library
d$stcon <- paste(d$state, d$county, sep=",")

#Color bins
colors = brewer.pal(5, "PuBu")
d$colorBuckets <- as.factor(as.numeric(cut(d$count,c(0,10,20,30,40,50,300))))

Here is my matching

mapnames <- map("county",plot=FALSE)[4]$names
colorsmatched <- d$colorBuckets [na.omit(match(mapnames ,d$stcon))]

Plotting:

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")

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:40:31+00:00Added an answer on May 14, 2026 at 7:40 am

    Here is a possible solution to your problem by merging your data with data from maps of select states. Is this what you were looking for?

    library(maps);
    library(RColorBrewer);
    
    # Create Dummy Data Frame to Play With
    
    d = rbind(c('fairfield','connecticut',17),c('westchester','new york',70), c('luzerne','pennsylvania',1));
    d = data.frame(d);
    names(d) = c("county", "state", "count");
    d$count = as.numeric(as.character(d$count));
    d$stcon = paste(d$state, d$county, sep=",");
    
    # Extract mapnames for States
    
    mapnames2 = map("county",c("new york","new jersey", "connecticut", "pennsylvania"),plot=FALSE)[4]$names;
    mapnames2 = data.frame(mapnames2);
    names(mapnames2) = "stcon";
    
    # Merge with d
    
    d = merge(mapnames2, d, all = T);
    d$count[is.na(d$count)] = 0;
    
    
    # Color bins
    colors = brewer.pal(5, "PuBu");
    d$colorBuckets = as.factor(as.numeric(cut(d$count,c(0,10,20,30,40,50,300))));
    
    map("county"
      ,c("new york","new jersey", "connecticut", "pennsylvania")
      ,col = colors[d$colorBuckets]
      ,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")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: Thx to suggestions from the mailing list I realized that the problem I
I recently have a problem with a crypto library which produces bad md5 output.
[edit] We're collecting credit application data from users on a web form. I have
Edit: I have solved this by myself. See my answer below I have set
EDIT: I have submitted a bug report and Microsoft have acknowledge that it is
EDIT: I have added Slug column to address performance issues on specific record selection.
EDIT: I have fixed all but two warnings now, so thank you all for
EDIT: I have edited my post... Working on a project (c#), I have a
EDIT i have something like this in a file: imagecolor=0 arrayimagecolorcopy=0 arrayimagecolorcopy3d=0 when i
I won't want to have edit any working sets. I just want a way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.