

{"id":3498,"date":"2015-07-09T14:25:18","date_gmt":"2015-07-09T19:25:18","guid":{"rendered":"http:\/\/rud.is\/b\/?p=3498"},"modified":"2018-03-08T08:16:36","modified_gmt":"2018-03-08T13:16:36","slug":"faceted-world-population-by-income-choropleths-in-ggplot","status":"publish","type":"post","link":"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/","title":{"rendered":"Faceted &#8220;World Population by Income&#8221; Choropleths in ggplot"},"content":{"rendered":"<p>Poynter did a nice <a href=\"http:\/\/www.pewglobal.org\/interactives\/global-population-by-income\/\">interactive piece<\/a> on world population by income (i.e. <em>&#8220;How Many Live on How Much, and Where&#8221;<\/em>). I&#8217;m always on the lookout for optimized shapefiles and clean data (I&#8217;m teaching a data science certificate program starting this Fall) and the speed of the site load and the easy availability of the data set made this one a &#8220;must acquire&#8221;. Rather than just repeat Poynter&#8217;s D3-goodness, here&#8217;s a way to look at the income data in series of small multiple choropleths&mdash;using R &amp; <code>ggplot2<\/code>&mdash;that involves:<\/p>\n<ul>\n<li>downloading data &amp; shapefiles from a web site<\/li>\n<li>using <code>dplyr<\/code> &amp; <code>tidyr<\/code> for data munging<\/li>\n<li>applying custom fill color scale mapping in <code>ggplot<\/code><\/li>\n<li>ordering plots with a custom facet order (using <code>factor<\/code>s)<\/li>\n<li>tweaking the <code>theme<\/code> and <code>aes<\/code>thetics for a nicely finished result<\/li>\n<\/ul>\n<p>By using D3, Poynter inherently made the data available. Pop open the &#8220;Developer Tools&#8221; in any browser, reload the page and look at the &#8220;Network&#8221; tab and you&#8217;ll see a list of files (you can sometimes see things in the source code, but this technique is often faster). The income data is a well-formed CSV file <a href=\"http:\/\/www.pewglobal.org\/wp-content\/themes\/pew-global\/interactive-global-class.csv\">http:\/\/www.pewglobal.org\/wp-content\/themes\/pew-global\/interactive-global-class.csv<\/a> and their highly optimized world map was also easy to discern <a href=\"http:\/\/www.pewglobal.org\/wp-content\/lib\/js\/world-geo.json\">http:\/\/www.pewglobal.org\/wp-content\/lib\/js\/world-geo.json<\/a>. We&#8217;ll start by grabbing the map and using the same map projection that Poynter did (Robinson). Don&#8217;t be put off by all the <code>library<\/code> calls since one of the best parts of R is the ever-increasing repository of great packages to help you get things done.<\/p>\n<pre lang=\"rsplus\">library(httr)     # getting data\nlibrary(rgdal)    # working with shapefile\nlibrary(dplyr)    # awesome data manipulation\nlibrary(readr)    # faster reading of CSV data\nlibrary(stringi)  # string manipulation\nlibrary(stringr)  # string manipulation\nlibrary(tidyr)    # reshaping data\nlibrary(grid)     # for 'unit'\nlibrary(scales)   # for 'percent'\nlibrary(ggplot2)  # plotting\nlibrary(ggthemes) # theme_map\n\n# this ensures you only download the shapefile once and hides\n# errors and warnings. remove `try` and `invisible` to see messages\ntry(invisible(GET(\"http:\/\/www.pewglobal.org\/wp-content\/lib\/js\/world-geo.json\",\n                  write_disk(\"world-geo.json\"))), silent=TRUE)\n\n# use ogrListLayers(\"world-geo.json\") to see file type & \n# layer info to use in the call to readOGR\n\nworld <- readOGR(\"world-geo.json\", \"OGRGeoJSON\")\nworld_wt <- spTransform(world, CRS(\"+proj=robin\"))\nworld_map <- fortify(world_wt)<\/pre>\n<p>I would have <em>liked<\/em> to do <code>fortify(world_wt, region=\"name\")<\/code> (since that makes working with filling in countries by name much easier in the choropleth part of the code) but that generated <code>TopologyException<\/code> errors (I've seen this happen quite a bit with simplified\/optimized shapefiles and some non-D3 geo-packages). One can sometimes fix those with a strategic <code>rgeos::gBuffer<\/code> call, but that didn't work well in this case. We can still use country names with a slight rejiggering of the fortified data frame using <code>dplyr<\/code>:<\/p>\n<pre lang=\"rsplus\">world_map %>%\n  left_join(data_frame(id=rownames(world@data), name=world@data$name)) %>%\n  select(-id) %>%\n  rename(id=name) -> world_map<\/pre>\n<p>Now it's time to get the data. The CSV file has annoying spaces in it that causes R to interpret all the columns as strings, so we can use <code>dplyr<\/code> again to get them into the format we want them in. Note that I'm also making the percentages decimals so we can use <code>percent<\/code> later on to easily format them.<\/p>\n<pre lang=\"rsplus\"># a good exercise would be to repeat the download code above \n# rather and make repeated calls to an external resource\nread_csv(\"http:\/\/www.pewglobal.org\/wp-content\/themes\/pew-global\/interactive-global-class.csv\") %>%\n  mutate_each(funs(str_trim)) %>%\n  filter(id != \"None\") %>%\n  mutate_each(funs(as.numeric(.)\/100), -name, -id) -> dat<\/pre>\n<p>For this post, we'll only be working with the actual share percentages, so let's:<\/p>\n<ul>\n<li>ignore the \"change\" columns<\/li>\n<li>convert the data frame from wide to long<\/li>\n<li>extract out the income levels (e.g. \"Poor\", \"Low Income\"...)<\/li>\n<li>set a factor order for them so our plots will be in the correct sequence<\/li>\n<\/ul>\n<pre lang=\"rsplus\">dat %>%\n  gather(share, value, starts_with(\"Share\"), -name, -id) %>%\n  select(-starts_with(\"Change\")) %>%\n  mutate(label=factor(stri_trans_totitle(str_match(share, \"Share ([[:alpha:]- ]+),\")[,2]),\n                      c(\"Poor\", \"Low Income\", \"Middle Income\", \"Upper-Middle Income\", \"High Income\"),\n                      ordered=TRUE)) -> share_dat<\/pre>\n<p>The <code>stringi<\/code> package is really handy (<code>stringr<\/code> is built on it, too). The <code>stri_trans_totitle<\/code> function alleviates some mundane string operations and the <code>stri_replace_all_regex<\/code> (below) also allows us to do vectorized regular expression replacements without a ton of code.<\/p>\n<p>To keep the charts aligned, we'll use Poynter's color scale (which was easy to extract from the site's code) and use the same legend breaks via `cut'. We'll also format the labels for these breaks to make our legend nicer to view.<\/p>\n<pre lang=\"rsplus\"># use same \"cuts\" as poynter\npoynter_scale_breaks <- c(0, 2.5, 5, 10, 25, 50, 75, 80, 100)\n\nsprintf(\"%2.1f-%s\", poynter_scale_breaks, percent(lead(poynter_scale_breaks\/100))) %>%\n  stri_replace_all_regex(c(\"^0.0\", \"-NA%\"), c(\"0\", \"%\"), vectorize_all=FALSE) %>%\n  head(-1) -> breaks_labels\n\nshare_dat %>%\n  mutate(`Share %`=cut(value,\n                       c(0, 2.5, 5, 10, 25, 50, 75, 80, 100)\/100,\n                       breaks_labels))-> share_dat\n\nshare_pal <- c(\"#eaecd8\", \"#d6dab3\", \"#c2c98b\", \"#949D48\", \"#6e7537\", \"#494E24\", \"#BB792A\", \"#7C441C\", \"#ffffff\")<\/pre>\n<p>Finally, we get to the good part and start plotting the visualization. There are only two layers: the base map and the choropleth filled map. We then:<\/p>\n<ul>\n<li>apply our manual color palette to them<\/li>\n<li>remove the line color slashes in the legend boxes<\/li>\n<li>setup the overall plot label<\/li>\n<li>tell <code>ggplot<\/code> which coordinate system to use (in this case <code>coord_equal<\/code> is fine since we already projected the points)<\/li>\n<li>apply a base theme that's good for mapping<\/li>\n<li>tweak the text and ensure our legend is in the position we want it to be ing<\/li>\n<\/ul>\n<pre lang=\"rsplus\">gg <- ggplot()\n\ngg <- gg + geom_map(data=world_map, map=world_map,\n                    aes(x=long, y=lat, group=group, map_id=id),\n                    color=\"#7f7f7f\", fill=\"white\", size=0.15)\n\ngg <- gg + geom_map(data=share_dat, map=world_map,\n                    aes(map_id=name, fill=`Share %`),\n                    color=\"#7f7f7f\", size=0.15)\n\ngg <- gg + scale_fill_manual(values=share_pal)\n\ngg <- gg + guides(fill=guide_legend(override.aes=list(colour=NA)))\ngg <- gg + labs(title=\"World Population by Income\\n\")\ngg <- gg + facet_wrap(~label, ncol=2)\n\ngg <- gg + coord_equal()\n\ngg <- gg + theme_map()\n\ngg <- gg + theme(panel.margin=unit(1, \"lines\"))\ngg <- gg + theme(plot.title=element_text(face=\"bold\", hjust=0, size=24))\ngg <- gg + theme(legend.title=element_text(face=\"bold\", hjust=0, size=12))\ngg <- gg + theme(legend.text=element_text(size=10))\ngg <- gg + theme(strip.text=element_text(face=\"bold\", size=10))\ngg <- gg + theme(strip.background=element_blank())\ngg <- gg + theme(legend.position=\"bottom\")\n\ngg<\/pre>\n<p>And, here's the result (click for larger version):<\/p>\n<p><a target=_blank href=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/07\/forblog.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"510\" height=\"510\" data-attachment-id=\"3500\" data-permalink=\"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/forblog\/\" data-orig-file=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/07\/forblog.png?fit=900%2C900&amp;ssl=1\" data-orig-size=\"900,900\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"forblog\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/07\/forblog.png?fit=300%2C300&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/07\/forblog.png?fit=510%2C510&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/07\/forblog.png?resize=510%2C510&#038;ssl=1\" alt=\"forblog\" class=\"aligncenter size-full wp-image-3500\" style=\"max-width:100%\" \/><\/a><\/p>\n<p>The optimized shapefile makes for a very fast plot and you can plot individual chorpleths by <code>filter<\/code>ing the data and not using facets.<\/p>\n<p>While there are a number of choropleth packages out there for R, learning how to do the core components on your own can (a) make you appreciate those packages a bit more (b) give you the skills to do them on your own when you need a more customized version. Many of the theme tweaks will also apply to the <code>ggplot<\/code>-based choropleth packages.<\/p>\n<p>With this base, it should be a fun exercise to the reader to do something similar with Poynter's \"percentage point change\" choropleth. You'll need to change the color palette and manipulate different data columns to get the same scales and visual representation they do. Drop a note in the comments if you give this a go!<\/p>\n<p>You can find all the code in this post in <a href=\"https:\/\/gist.github.com\/hrbrmstr\/5a9a0d93cbb54f8ce777\">one convenient gist<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Poynter did a nice interactive piece on world population by income (i.e. &#8220;How Many Live on How Much, and Where&#8221;). I&#8217;m always on the lookout for optimized shapefiles and clean data (I&#8217;m teaching a data science certificate program starting this Fall) and the speed of the site load and the easy availability of the data [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":""},"categories":[721,24,678,673,674,720,706,91],"tags":[810],"class_list":["post-3498","post","type-post","status-publish","format-standard","hentry","category-cartography","category-charts-graphs","category-data-visualization","category-datavis-2","category-dataviz","category-gis","category-maps","category-r","tag-post"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Faceted &quot;World Population by Income&quot; Choropleths in ggplot - rud.is<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Faceted &quot;World Population by Income&quot; Choropleths in ggplot - rud.is\" \/>\n<meta property=\"og:description\" content=\"Poynter did a nice interactive piece on world population by income (i.e. &#8220;How Many Live on How Much, and Where&#8221;). I&#8217;m always on the lookout for optimized shapefiles and clean data (I&#8217;m teaching a data science certificate program starting this Fall) and the speed of the site load and the easy availability of the data [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/\" \/>\n<meta property=\"og:site_name\" content=\"rud.is\" \/>\n<meta property=\"article:published_time\" content=\"2015-07-09T19:25:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-03-08T13:16:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/rud.is\/b\/wp-content\/uploads\/2015\/07\/forblog.png\" \/>\n<meta name=\"author\" content=\"hrbrmstr\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"hrbrmstr\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/\"},\"author\":{\"name\":\"hrbrmstr\",\"@id\":\"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886\"},\"headline\":\"Faceted &#8220;World Population by Income&#8221; Choropleths in ggplot\",\"datePublished\":\"2015-07-09T19:25:18+00:00\",\"dateModified\":\"2018-03-08T13:16:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/\"},\"wordCount\":804,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886\"},\"image\":{\"@id\":\"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/rud.is\/b\/wp-content\/uploads\/2015\/07\/forblog.png\",\"keywords\":[\"post\"],\"articleSection\":[\"cartography\",\"Charts &amp; Graphs\",\"Data Visualization\",\"DataVis\",\"DataViz\",\"gis\",\"maps\",\"R\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/\",\"url\":\"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/\",\"name\":\"Faceted \\\"World Population by Income\\\" Choropleths in ggplot - rud.is\",\"isPartOf\":{\"@id\":\"https:\/\/rud.is\/b\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/rud.is\/b\/wp-content\/uploads\/2015\/07\/forblog.png\",\"datePublished\":\"2015-07-09T19:25:18+00:00\",\"dateModified\":\"2018-03-08T13:16:36+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/07\/forblog.png?fit=900%2C900&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/07\/forblog.png?fit=900%2C900&ssl=1\",\"width\":900,\"height\":900},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/rud.is\/b\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Faceted &#8220;World Population by Income&#8221; Choropleths in ggplot\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/rud.is\/b\/#website\",\"url\":\"https:\/\/rud.is\/b\/\",\"name\":\"rud.is\",\"description\":\"&quot;In God we trust. All others must bring data&quot;\",\"publisher\":{\"@id\":\"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/rud.is\/b\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886\",\"name\":\"hrbrmstr\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2023\/10\/ukr-shield.png?fit=460%2C460&ssl=1\",\"url\":\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2023\/10\/ukr-shield.png?fit=460%2C460&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2023\/10\/ukr-shield.png?fit=460%2C460&ssl=1\",\"width\":460,\"height\":460,\"caption\":\"hrbrmstr\"},\"logo\":{\"@id\":\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2023\/10\/ukr-shield.png?fit=460%2C460&ssl=1\"},\"description\":\"Don't look at me\u2026I do what he does \u2014 just slower. #rstats avuncular \u2022 ?Resistance Fighter \u2022 Cook \u2022 Christian \u2022 [Master] Chef des Donn\u00e9es de S\u00e9curit\u00e9 @ @rapid7\",\"sameAs\":[\"http:\/\/rud.is\"],\"url\":\"https:\/\/rud.is\/b\/author\/hrbrmstr\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Faceted \"World Population by Income\" Choropleths in ggplot - rud.is","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/","og_locale":"en_US","og_type":"article","og_title":"Faceted \"World Population by Income\" Choropleths in ggplot - rud.is","og_description":"Poynter did a nice interactive piece on world population by income (i.e. &#8220;How Many Live on How Much, and Where&#8221;). I&#8217;m always on the lookout for optimized shapefiles and clean data (I&#8217;m teaching a data science certificate program starting this Fall) and the speed of the site load and the easy availability of the data [&hellip;]","og_url":"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/","og_site_name":"rud.is","article_published_time":"2015-07-09T19:25:18+00:00","article_modified_time":"2018-03-08T13:16:36+00:00","og_image":[{"url":"https:\/\/rud.is\/b\/wp-content\/uploads\/2015\/07\/forblog.png","type":"","width":"","height":""}],"author":"hrbrmstr","twitter_card":"summary_large_image","twitter_misc":{"Written by":"hrbrmstr","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/#article","isPartOf":{"@id":"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/"},"author":{"name":"hrbrmstr","@id":"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886"},"headline":"Faceted &#8220;World Population by Income&#8221; Choropleths in ggplot","datePublished":"2015-07-09T19:25:18+00:00","dateModified":"2018-03-08T13:16:36+00:00","mainEntityOfPage":{"@id":"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/"},"wordCount":804,"commentCount":5,"publisher":{"@id":"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886"},"image":{"@id":"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/#primaryimage"},"thumbnailUrl":"https:\/\/rud.is\/b\/wp-content\/uploads\/2015\/07\/forblog.png","keywords":["post"],"articleSection":["cartography","Charts &amp; Graphs","Data Visualization","DataVis","DataViz","gis","maps","R"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/","url":"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/","name":"Faceted \"World Population by Income\" Choropleths in ggplot - rud.is","isPartOf":{"@id":"https:\/\/rud.is\/b\/#website"},"primaryImageOfPage":{"@id":"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/#primaryimage"},"image":{"@id":"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/#primaryimage"},"thumbnailUrl":"https:\/\/rud.is\/b\/wp-content\/uploads\/2015\/07\/forblog.png","datePublished":"2015-07-09T19:25:18+00:00","dateModified":"2018-03-08T13:16:36+00:00","breadcrumb":{"@id":"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/#primaryimage","url":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/07\/forblog.png?fit=900%2C900&ssl=1","contentUrl":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/07\/forblog.png?fit=900%2C900&ssl=1","width":900,"height":900},{"@type":"BreadcrumbList","@id":"https:\/\/rud.is\/b\/2015\/07\/09\/faceted-world-population-by-income-choropleths-in-ggplot\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/rud.is\/b\/"},{"@type":"ListItem","position":2,"name":"Faceted &#8220;World Population by Income&#8221; Choropleths in ggplot"}]},{"@type":"WebSite","@id":"https:\/\/rud.is\/b\/#website","url":"https:\/\/rud.is\/b\/","name":"rud.is","description":"&quot;In God we trust. All others must bring data&quot;","publisher":{"@id":"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/rud.is\/b\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886","name":"hrbrmstr","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2023\/10\/ukr-shield.png?fit=460%2C460&ssl=1","url":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2023\/10\/ukr-shield.png?fit=460%2C460&ssl=1","contentUrl":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2023\/10\/ukr-shield.png?fit=460%2C460&ssl=1","width":460,"height":460,"caption":"hrbrmstr"},"logo":{"@id":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2023\/10\/ukr-shield.png?fit=460%2C460&ssl=1"},"description":"Don't look at me\u2026I do what he does \u2014 just slower. #rstats avuncular \u2022 ?Resistance Fighter \u2022 Cook \u2022 Christian \u2022 [Master] Chef des Donn\u00e9es de S\u00e9curit\u00e9 @ @rapid7","sameAs":["http:\/\/rud.is"],"url":"https:\/\/rud.is\/b\/author\/hrbrmstr\/"}]}},"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p23idr-Uq","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":4217,"url":"https:\/\/rud.is\/b\/2016\/03\/29\/easier-composite-u-s-choropleths-with-albersusa\/","url_meta":{"origin":3498,"position":0},"title":"Easier Composite U.S. Choropleths with albersusa","author":"hrbrmstr","date":"2016-03-29","format":false,"excerpt":"Folks who've been tracking this blog on R-bloggers probably remember [this post](https:\/\/rud.is\/b\/2014\/11\/16\/moving-the-earth-well-alaska-hawaii-with-r\/) where I showed how to create a composite U.S. map with an Albers projection (which is commonly referred to as AlbersUSA these days thanks to D3). I'm not sure why I didn't think of this earlier, but you\u2026","rel":"","context":"In &quot;cartography&quot;","block_context":{"text":"cartography","link":"https:\/\/rud.is\/b\/category\/cartography\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2016\/03\/Fullscreen_3_29_16__9_06_AM.png?fit=1200%2C747&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2016\/03\/Fullscreen_3_29_16__9_06_AM.png?fit=1200%2C747&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2016\/03\/Fullscreen_3_29_16__9_06_AM.png?fit=1200%2C747&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2016\/03\/Fullscreen_3_29_16__9_06_AM.png?fit=1200%2C747&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2016\/03\/Fullscreen_3_29_16__9_06_AM.png?fit=1200%2C747&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3158,"url":"https:\/\/rud.is\/b\/2014\/12\/29\/making-static-interactive-maps-with-ggvis-using-ggvis-maps-wshiny\/","url_meta":{"origin":3498,"position":1},"title":"Making Static &#038; Interactive Maps With ggvis (+ using ggvis maps w\/shiny)","author":"hrbrmstr","date":"2014-12-29","format":false,"excerpt":"Even though it's still at version `0.4`, the `ggvis` package has quite a bit of functionality and is highly useful for exploratory data analysis (EDA). I wanted to see how geographical visualizations would work under it, so I put together six examples that show how to use various features of\u2026","rel":"","context":"In &quot;cartography&quot;","block_context":{"text":"cartography","link":"https:\/\/rud.is\/b\/category\/cartography\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3700,"url":"https:\/\/rud.is\/b\/2015\/10\/04\/replicating-natgeos-proper-earthquake-map-in-r\/","url_meta":{"origin":3498,"position":2},"title":"Replicating NatGeo&#8217;s &#8220;Proper&#8221; Earthquake Map in R","author":"hrbrmstr","date":"2015-10-04","format":false,"excerpt":"I saw this post over at NatGeo over the weekend and felt compelled to replicate this: with ggplot2. Three shapefiles later and we have it close enough to toss into a post (and I really don't believe the continent names are necessary). library(rgdal) library(ggplot2) library(ggthemes) library(ggalt) # devtools::install_github(\"hrbrmstr\/ggalt\") # grab\u2026","rel":"","context":"In &quot;cartography&quot;","block_context":{"text":"cartography","link":"https:\/\/rud.is\/b\/category\/cartography\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/10\/unnamed-chunk-1-1.png?fit=1200%2C685&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/10\/unnamed-chunk-1-1.png?fit=1200%2C685&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/10\/unnamed-chunk-1-1.png?fit=1200%2C685&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/10\/unnamed-chunk-1-1.png?fit=1200%2C685&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/10\/unnamed-chunk-1-1.png?fit=1200%2C685&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3538,"url":"https:\/\/rud.is\/b\/2015\/07\/24\/a-path-towards-easier-map-projection-machinations-with-ggplot2\/","url_meta":{"origin":3498,"position":3},"title":"A Path Towards Easier Map Projection Machinations with ggplot2","author":"hrbrmstr","date":"2015-07-24","format":false,"excerpt":"The $DAYJOB doesn't afford much opportunity to work with cartographic datasets, but I really like maps and tinker with shapefiles and geo-data when I can, plus answer a ton of geo-questions on StackOverflow. R makes it easy\u2014one might even say too easy\u2014to work with maps. All it takes to make\u2026","rel":"","context":"In &quot;cartography&quot;","block_context":{"text":"cartography","link":"https:\/\/rud.is\/b\/category\/cartography\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2949,"url":"https:\/\/rud.is\/b\/2014\/04\/01\/mapping-the-march-2014-california-earthquake-with-ggmap\/","url_meta":{"origin":3498,"position":4},"title":"Mapping the March 2014 California Earthquake with ggmap","author":"hrbrmstr","date":"2014-04-01","format":false,"excerpt":"I had no intention to blog this, but @jayjacobs convinced me otherwise. I was curious about the recent (end of March, 2014) [California earthquake](http:\/\/www.latimes.com\/local\/lanow\/la-me-ln-an-estimated-17-million-people-felt-51-earthquake-in-california-20140331,0,2465821.story#axzz2xfGBteq0) \"storm\" and did a quick plot for \"fun\" and personal use using `ggmap`\/`ggplot`. I used data from the [Southern California Earthquake Center](http:\/\/www.data.scec.org\/recent\/recenteqs\/Maps\/Los_Angeles.html) (that I cleaned up\u2026","rel":"","context":"In &quot;Data Visualization&quot;","block_context":{"text":"Data Visualization","link":"https:\/\/rud.is\/b\/category\/data-visualization\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4574,"url":"https:\/\/rud.is\/b\/2016\/07\/27\/u-s-drought-animations-with-the-witchs-brew-purrr-broom-magick\/","url_meta":{"origin":3498,"position":5},"title":"U.S. Drought Animations with the &#8220;Witch&#8217;s Brew&#8221; (purrr + broom + magick)","author":"hrbrmstr","date":"2016-07-27","format":false,"excerpt":"This is another purrr-focused post but it's also an homage to the nascent magick package (R interface to ImageMagick) by @opencpu. We're starting to see\/feel the impact of the increasing drought up here in southern Maine. I've used the data from the U.S. Drought Monitor before on the blog, but\u2026","rel":"","context":"In &quot;cartography&quot;","block_context":{"text":"cartography","link":"https:\/\/rud.is\/b\/category\/cartography\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/posts\/3498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/comments?post=3498"}],"version-history":[{"count":0,"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/posts\/3498\/revisions"}],"wp:attachment":[{"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/media?parent=3498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/categories?post=3498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/tags?post=3498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}