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

  • Home
  • SEARCH
  • 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 7438459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:33:27+00:00 2026-05-29T10:33:27+00:00

Using Hadley’s great ggplot2 and his book (pp. 78-79), I am able to produce

  • 0

Using Hadley’s great ggplot2 and his book (pp. 78-79), I am able to produce single choropleth map plots with ease, using code like this:

states.df <- map_data("state")
states.df = subset(states.df,group!=8) # get rid of DC
states.df$st <- state.abb[match(states.df$region,tolower(state.name))] # attach state abbreviations

states.df$value = value[states.df$st]

p = qplot(long, lat, data = states.df, group = group, fill = value, geom = "polygon", xlab="", ylab="", main=main) + opts(axis.text.y=theme_blank(), axis.text.x=theme_blank(), axis.ticks = theme_blank()) + scale_fill_continuous (name)
p2 = p + geom_path(data=states.df, color = "white", alpha = 0.4, fill = NA) + coord_map(project="polyconic")

Where “value” is the vector of state-level data I am plotting. But what if I want to plot multiple maps, grouped by some variable (or two)?

Here’s an example of a plot done by Andrew Gelman, later adapted in the New York Times, about health care opinion in the states:

enter image description here

I’d love to be able to emulate this example: show choropleth plots gridded according to two variables (or even one). So I pass not a vector of values, but rather a dataframe organized “long”, with multiple entries for each state.

I know ggplot2 can do this, but I’m not sure how. Thanks!

  • 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-29T10:33:28+00:00Added an answer on May 29, 2026 at 10:33 am

    You can add two columns for the desired groupings and use facets:

    library(ggplot2)
    library(maps)
    d1 <- map_data("state")
    d2 <- unique(d1$group)
    n <- length(d2)
    d2 <- data.frame( 
      group=rep(d2,each=6), 
      g1=rep(1:3,each=2,length=6*n),
      g2=rep(1:2,length=6*n),
      value=runif(6*n)
    )
    d <- merge(d1, d2,  by="group")
    qplot(
      long, lat, data = d, group = group, 
      fill = value, geom = "polygon" 
    ) + 
      facet_wrap( ~ g1 + g2 )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using geom_smooth() from ggplot2 . In Hadley Wickham's book (ggplot2 - Elegant Graphics
Using the Redis info command, I am able to get all the stats of
I am using the toolbox theme for wordpress. This is the code used to
Hadley turned me on to the plyr package and I find myself using it
I've been using Emacs/ESS for quite a while, and I'm familiar with Hadley's R
I am trying to replicate Hadley Wickham's example on page 86 of the ggplot2
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
Using a populated Table Type as the source for a TSQL-Merge. I want to
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using SQL Server 2008 R2 we are looking for a way to select the

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.