Note that there are some explanatory texts on larger screens.

plurals
  1. POChoropleth Map in ggplot2
    primarykey
    data
    text
    <p>I'm trying to reproduce the Choropleth Map given <a href="http://blog.revolutionanalytics.com/2009/11/choropleth-challenge-result.html" rel="nofollow">here</a> with the code provided <a href="https://gist.github.com/hadley/233134" rel="nofollow">by Hadley</a>.</p> <pre><code>library(ggplot2) library(maps) # First (and most annoying) task - get matching state and county variables # for both datasets. And unfortauntely it's not quite right, as you can # see from the finish product - some counties are missing. unemp &lt;- read.csv("unemployment09.csv", header = F, stringsAsFactors = F) names(unemp) &lt;- c("id", "state_fips", "county_fips", "name", "year", "?", "?", "?", "rate") unemp$county &lt;- tolower(gsub(" County, [A-Z]{2}", "", unemp$name)) unemp$state &lt;- gsub("^.*([A-Z]{2}).*$", "\\1", unemp$name) county_df &lt;- map_data("county") names(county_df) &lt;- c("long", "lat", "group", "order", "state_name", "county") county_df$state &lt;- state.abb[match(county_df$state_name, tolower(state.name))] county_df$state_name &lt;- NULL state_df &lt;- map_data("state") # Combine together choropleth &lt;- merge(county_df, unemp, by = c("state", "county")) choropleth &lt;- choropleth[order(choropleth$order), ] # Discretise rate to use with Brewer colour scheme - many options here # choropleth$rate_d &lt;- cut_number(choropleth$rate, 5) # choropleth$rate_d &lt;- cut_interval(choropleth$rate, 5) # Nathan's choice is a little odd: choropleth$rate_d &lt;- cut(choropleth$rate, breaks = c(seq(0, 10, by = 2), 35)) # Once you have the data in the right format, recreating the plot is straight # forward. ggplot(choropleth, aes(long, lat, group = group)) + geom_polygon(aes(fill = rate_d), colour = alpha("white", 1/2), size = 0.2) + geom_polygon(data = state_df, colour = "white", fill = NA) + scale_fill_brewer(pal = "PuRd") </code></pre> <p>But this code gives the following error:</p> <blockquote> <pre><code>Error in do.call("layer", list(mapping = mapping, data = data, stat = stat, : could not find function "alpha" </code></pre> </blockquote> <p>Deleting alpha and using this code </p> <pre><code>ggplot(choropleth, aes(long, lat, group = group)) + geom_polygon(aes(fill = rate_d), colour = "white", size = 0.2) + geom_polygon(data = state_df, colour = "white", fill = NA) + scale_fill_brewer(pal = "PuRd") </code></pre> <p>gives the following error:</p> <blockquote> <p>Error in scale_map.discrete(scale, df[[j]]) : attempt to apply non-function</p> </blockquote> <p>I wonder how I can fix this problem. Any help will be highly appreciated. Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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