

{"id":3649,"date":"2015-08-27T09:36:08","date_gmt":"2015-08-27T14:36:08","guid":{"rendered":"http:\/\/rud.is\/b\/?p=3649"},"modified":"2018-03-07T16:43:28","modified_gmt":"2018-03-07T21:43:28","slug":"coloring-and-drawing-outside-the-lines-in-ggplot","status":"publish","type":"post","link":"https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/","title":{"rendered":"Coloring (and Drawing) Outside the Lines in ggplot"},"content":{"rendered":"<p>Time for another Twitter-inspired blog post this week, this time from a tweet by @JonKalodimos:<\/p>\n<blockquote class=\"twitter-tweet\" lang=\"en\">\n<p lang=\"en\" dir=\"ltr\">Is there a way to do this in <a href=\"https:\/\/mobile.twitter.com\/hashtag\/rstats?src=hash\">#rstats<\/a> <a href=\"https:\/\/mobile.twitter.com\/hashtag\/ggplot2?src=hash\">#ggplot2<\/a> <a href=\"https:\/\/t.co\/kxWQFlYpbB\">https:\/\/t.co\/kxWQFlYpbB<\/a><\/p>\n<p>&mdash; Jonathan Kalodimos (@JonKalodimos) <a href=\"https:\/\/mobile.twitter.com\/JonKalodimos\/status\/636880959191826432\">August 27, 2015<\/a><\/p><\/blockquote>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n<p>I had seen and appreciated Ann&#8217;s post on her makeover of the main graphic in [NPR&#8217;s story](http:\/\/www.npr.org\/sections\/money\/2014\/10\/21\/357629765\/when-women-stopped-coding) and did a quick mental check of how I&#8217;d do the same in ggplot2 as I was reading it. Jon&#8217;s question was a good prompt to dump physical memory to internet memory.<\/p>\n<p>Here&#8217;s the NPR graphic:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3650\" data-permalink=\"https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/when_women_stopped_coding___planet_money___npr\/\" data-orig-file=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/08\/When_Women_Stopped_Coding___Planet_Money___NPR.png?fit=702%2C514&amp;ssl=1\" data-orig-size=\"702,514\" 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=\"When_Women_Stopped_Coding___Planet_Money___NPR\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/08\/When_Women_Stopped_Coding___Planet_Money___NPR.png?fit=510%2C373&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/08\/When_Women_Stopped_Coding___Planet_Money___NPR.png?resize=510%2C373&#038;ssl=1\" alt=\"When_Women_Stopped_Coding___Planet_Money___NPR\" width=\"510\" height=\"373\" class=\"aligncenter size-full wp-image-3650\" \/><\/p>\n<p>It is actually pretty darn good on it&#8217;s own, but I also agree with Ann that direct labeling could have made it better. Here&#8217;s her makeover:<\/p>\n<p><center><img src=http:\/\/i1.wp.com\/annkemery.com\/wp-content\/uploads\/2015\/09\/emery-labeling-line-graphs-5.png?resize=600%2C308\/><\/center><\/p>\n<p>Let&#8217;s see how to do this in ggplot2. We&#8217;ll use the actual data from NPR&#8217;s story since the graphic was built with D3 and, hence, the data is part of the graphic. Let&#8217;s get the `library` stuff out of the way:<\/p>\n<pre lang=\"rsplus\">library(ggplot2)\r\nlibrary(dplyr)\r\nlibrary(tidyr)\r\nlibrary(stringr)\r\nlibrary(scales)\r\nlibrary(gridExtra)\r\nlibrary(grid)<\/pre>\n<p>Now, we&#8217;ll grab the CSV that the NPR folks used for the graphic and take a look at it. I found it via Developer Tools in Chrome:<\/p>\n<pre lang=\"rsplus\">\r\n# use the NPR story data file ---------------------------------------------\r\n# and be kind to NPR's bandwidth budget\r\nurl <- \"http:\/\/apps.npr.org\/dailygraphics\/graphics\/women-cs\/data.csv\"\r\nfil <- \"gender.csv\"\r\nif (!file.exists(fil)) download.file(url, fil)\r\n\r\ngender <- read.csv(fil, stringsAsFactors=FALSE)\r\n\r\n# take a look at the CSV structure ----------------------------------------\r\n\r\nglimpse(gender)\r\n\r\n## Observations: 48\r\n## Variables:\r\n## $ date              (int) 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, ...\r\n## $ Medical.School    (dbl) 0.09, 0.10, 0.10, 0.09, 0.09, 0.11, 0.14, 0.17, 0.20, 0.22, 0.24, 0.25, 0.25, 0.25, 0.28, 0.29, 0.31, ...\r\n## $ Law.School        (chr) \"0.04\", \"0.04\", \"0.05\", \"0.07\", \"0.07\", \"0.1\", \"0.12\", \"0.16\", \"0.2\", \"0.24\", \"0.27\", \"0.28\", \"0.3\", \"...\r\n## $ Physical.Sciences (chr) \"0.14\", \"0.14\", \"0.14\", \"0.14\", \"0.14\", \"0.15\", \"0.16\", \"0.16\", \"0.17\", \"0.19\", \"0.2\", \"0.2\", \"0.22\", ...\r\n## $ Computer.science  (dbl) 0.146, 0.108, 0.120, 0.130, 0.129, 0.136, 0.136, 0.149, 0.164, 0.190, 0.198, 0.239, 0.258, 0.281, 0.30...\r\n\r\ntail(gender)\r\n\r\n##    date Medical School Law School Physical Sciences Computer science\r\n## 43 2008           0.48       0.47              0.41            0.177\r\n## 44 2009           0.48       0.47              0.42            0.179\r\n## 45 2010           0.48       0.47              0.41            0.182\r\n## 46 2011           0.47         tk                tk            0.177\r\n## 47 2012           0.47         tk                tk            0.182\r\n## 48 2013           0.46         tk                              0.179<\/pre>\n<p>Those `tk` values are referred to in the [code that makes the NPR graphic](http:\/\/apps.npr.org\/dailygraphics\/graphics\/women-cs\/js\/graphic.js) so we'll replace them with `NA`s and make all the columns numeric:<\/p>\n<pre lang=\"rsplus\">gender <- mutate_each(gender, funs(as.numeric))<\/pre>\n<p>We should also clean up the column names since we'll be using them for the legend and the direct labels:<\/p>\n<pre lang=\"rsplus\">\r\ncolnames(gender) <- str_replace(colnames(gender), \"\\\\.\", \" \")\r\n\r\ngender_long <- mutate(gather(gender, area, value, -date),\r\n                      area=factor(area, levels=colnames(gender)[2:5],\r\n                                  ordered=TRUE))<\/pre>\n<p>That that code link also has the colors NPR used for the graphic, so let's define those now since we bothered to look at it:<\/p>\n<pre lang=\"rsplus\">gender_colors <- c('#11605E', '#17807E', '#8BC0BF','#D8472B')\r\nnames(gender_colors) <- colnames(gender)[2:5]<\/pre>\n<p>We'll be needing those names later on, hence why I named the values in the vector.<\/p>\n<p>With the data, labels and colors defined, we can make a \"standard\" ggplot:<\/p>\n<pre lang=\"rsplus\">chart_title <- expression(atop(\"What Happened To Women In Computer Science?\",\r\n                               atop(italic(\"% Of Women Majors, By Field\"))))\r\n\r\ngg <- ggplot(gender_long)\r\ngg <- gg + geom_line(aes(x=date, y=value, group=area, color=area))\r\ngg <- gg + scale_color_manual(name=\"\", values=gender_colors)\r\ngg <- gg + scale_y_continuous(label=percent)\r\ngg <- gg + labs(x=NULL, y=NULL, title=chart_title)\r\ngg <- gg + theme_bw(base_family=\"Helvetica\")\r\ngg <- gg + theme(axis.ticks.y=element_blank())\r\ngg <- gg + theme(panel.border=element_blank())\r\ngg <- gg + theme(legend.key=element_blank())\r\ngg<\/pre>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3652\" data-permalink=\"https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/rplot01-4\/\" data-orig-file=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/08\/Rplot01.png?fit=968%2C530&amp;ssl=1\" data-orig-size=\"968,530\" 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=\"Rplot01\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/08\/Rplot01.png?fit=510%2C279&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/08\/Rplot01.png?resize=510%2C279&#038;ssl=1\" alt=\"Rplot01\" width=\"510\" height=\"279\" class=\"aligncenter size-full wp-image-3652\" \/><\/p>\n<p>That's also pretty good on it's own. It's possible to make it look a bit more like the NPR chart, but it's hard to format a title & subtitle in a ggplot title _and_ have it left-justified, so I opted for font style. It's also possible to make the legend look like NPR's but that's not the point of the post.<\/p>\n<p>So, how do we make this look more like Ann's makeover?<\/p>\n<p>First we need to get the last values for each of the variables so we know what point on the `y` axis we need to place the labels. That's made a bit trickier with the `NA`s:<\/p>\n<pre lang=\"rsplus\">last_vals <- sapply(colnames(gender)[2:5], function(x) last(na.exclude(gender[,x])))\r\nlast_date <- tail(gender$date)+1 # doing this ^ wld have made it a double<\/pre>\n<p>Next, we need to turn off the legend and increase the plot margin on the right-hand side:<\/p>\n<pre lang=\"rsplus\">gg <- gg + theme(legend.position=\"none\")\r\ngg <- gg + theme(plot.margin = unit(c(1, 7, 2, 1), \"lines\"))<\/pre>\n<p>I figured out those #'s by interactive trial-and-error, though I initially guessed `6` for the right-hand margin increase. Also, this should demonstrate one reason for the `gg <- gg +` madness you see in my code\/posts since, when you start doing more in ggplot, you end up with that idiom more oft than not.\n\nNow, we add the labels. We do it with with custom annotations that are placed \"one year\" after the latest `x` value and at the same `y` value as the last reading of each area. We also color the label the same as the line, which is why we needed a named vector.\n\n\n\n<pre lang=\"rsplus\">\r\nfor (i in 1:length(last_vals)) {\r\n  gg <- gg + annotation_custom(grob=textGrob(names(last_vals)[i], hjust=0,\r\n                                             gp=gpar(fontsize=8, \r\n                                                     col=gender_colors[names(last_vals)[i]])),\r\n                               xmin=2014, xmax=2014,\r\n                               ymin=last_vals[i], ymax=last_vals[i])\r\n}<\/pre>\n<p>Finally, we have to do some of the remaining work by hand since we have to turn off panel clipping and the only way I know how to do that is at the grob\/gtable level, but it's not that scary or complex of a task. Also, since we are manipulating the built ggplot object, we have to use `grid.draw` to present our chart:<\/p>\n<pre lang=\"rsplus\">gb <- ggplot_build(gg)\r\ngt <- ggplot_gtable(gb)\r\n\r\ngt$layout$clip[gt$layout$name==\"panel\"] <- \"off\"\r\n\r\ngrid.draw(gt)<\/pre>\n<p>Here's the result:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3653\" data-permalink=\"https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/rplot02-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/08\/Rplot02.png?fit=968%2C530&amp;ssl=1\" data-orig-size=\"968,530\" 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=\"Rplot02\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/08\/Rplot02.png?fit=510%2C279&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/08\/Rplot02.png?resize=510%2C279&#038;ssl=1\" alt=\"Rplot02\" width=\"510\" height=\"279\" class=\"aligncenter size-full wp-image-3653\" \/><\/p>\n<p>I've deliberately left the fonts a bit small and not-changed their positions on the `y`-axis to give readers a bit of homework. They both _should_ be changed and the plot margins could also be tweaked a tad. You can find the complete code [on github](https:\/\/gist.github.com\/hrbrmstr\/83deb0baeabae0824389) so tweak away!<\/p>\n<p>If you have another way to accomplish the same task or want to show off your tweaked version, drop a note in the comments or at that gist link.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Time for another Twitter-inspired blog post this week, this time from a tweet by @JonKalodimos: Is there a way to do this in #rstats #ggplot2 https:\/\/t.co\/kxWQFlYpbB &mdash; Jonathan Kalodimos (@JonKalodimos) August 27, 2015 I had seen and appreciated Ann&#8217;s post on her makeover of the main graphic in [NPR&#8217;s story](http:\/\/www.npr.org\/sections\/money\/2014\/10\/21\/357629765\/when-women-stopped-coding) and did a quick mental [&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":true,"_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":[678,673,674,753,91],"tags":[810],"class_list":["post-3649","post","type-post","status-publish","format-standard","hentry","category-data-visualization","category-datavis-2","category-dataviz","category-ggplot","category-r","tag-post"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Coloring (and Drawing) Outside the Lines 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\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Coloring (and Drawing) Outside the Lines in ggplot - rud.is\" \/>\n<meta property=\"og:description\" content=\"Time for another Twitter-inspired blog post this week, this time from a tweet by @JonKalodimos: Is there a way to do this in #rstats #ggplot2 https:\/\/t.co\/kxWQFlYpbB &mdash; Jonathan Kalodimos (@JonKalodimos) August 27, 2015 I had seen and appreciated Ann&#8217;s post on her makeover of the main graphic in [NPR&#8217;s story](http:\/\/www.npr.org\/sections\/money\/2014\/10\/21\/357629765\/when-women-stopped-coding) and did a quick mental [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/\" \/>\n<meta property=\"og:site_name\" content=\"rud.is\" \/>\n<meta property=\"article:published_time\" content=\"2015-08-27T14:36:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-03-07T21:43:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/rud.is\/b\/wp-content\/uploads\/2015\/08\/When_Women_Stopped_Coding___Planet_Money___NPR.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=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/rud.is\\\/b\\\/2015\\\/08\\\/27\\\/coloring-and-drawing-outside-the-lines-in-ggplot\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/rud.is\\\/b\\\/2015\\\/08\\\/27\\\/coloring-and-drawing-outside-the-lines-in-ggplot\\\/\"},\"author\":{\"name\":\"hrbrmstr\",\"@id\":\"https:\\\/\\\/rud.is\\\/b\\\/#\\\/schema\\\/person\\\/d7cb7487ab0527447f7fda5c423ff886\"},\"headline\":\"Coloring (and Drawing) Outside the Lines in ggplot\",\"datePublished\":\"2015-08-27T14:36:08+00:00\",\"dateModified\":\"2018-03-07T21:43:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/rud.is\\\/b\\\/2015\\\/08\\\/27\\\/coloring-and-drawing-outside-the-lines-in-ggplot\\\/\"},\"wordCount\":483,\"commentCount\":8,\"publisher\":{\"@id\":\"https:\\\/\\\/rud.is\\\/b\\\/#\\\/schema\\\/person\\\/d7cb7487ab0527447f7fda5c423ff886\"},\"image\":{\"@id\":\"https:\\\/\\\/rud.is\\\/b\\\/2015\\\/08\\\/27\\\/coloring-and-drawing-outside-the-lines-in-ggplot\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/rud.is\\\/b\\\/wp-content\\\/uploads\\\/2015\\\/08\\\/When_Women_Stopped_Coding___Planet_Money___NPR.png\",\"keywords\":[\"post\"],\"articleSection\":[\"Data Visualization\",\"DataVis\",\"DataViz\",\"ggplot\",\"R\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/rud.is\\\/b\\\/2015\\\/08\\\/27\\\/coloring-and-drawing-outside-the-lines-in-ggplot\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/rud.is\\\/b\\\/2015\\\/08\\\/27\\\/coloring-and-drawing-outside-the-lines-in-ggplot\\\/\",\"url\":\"https:\\\/\\\/rud.is\\\/b\\\/2015\\\/08\\\/27\\\/coloring-and-drawing-outside-the-lines-in-ggplot\\\/\",\"name\":\"Coloring (and Drawing) Outside the Lines in ggplot - rud.is\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/rud.is\\\/b\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/rud.is\\\/b\\\/2015\\\/08\\\/27\\\/coloring-and-drawing-outside-the-lines-in-ggplot\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/rud.is\\\/b\\\/2015\\\/08\\\/27\\\/coloring-and-drawing-outside-the-lines-in-ggplot\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/rud.is\\\/b\\\/wp-content\\\/uploads\\\/2015\\\/08\\\/When_Women_Stopped_Coding___Planet_Money___NPR.png\",\"datePublished\":\"2015-08-27T14:36:08+00:00\",\"dateModified\":\"2018-03-07T21:43:28+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/rud.is\\\/b\\\/2015\\\/08\\\/27\\\/coloring-and-drawing-outside-the-lines-in-ggplot\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/rud.is\\\/b\\\/2015\\\/08\\\/27\\\/coloring-and-drawing-outside-the-lines-in-ggplot\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/rud.is\\\/b\\\/2015\\\/08\\\/27\\\/coloring-and-drawing-outside-the-lines-in-ggplot\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/rud.is\\\/b\\\/wp-content\\\/uploads\\\/2015\\\/08\\\/When_Women_Stopped_Coding___Planet_Money___NPR.png?fit=702%2C514&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/rud.is\\\/b\\\/wp-content\\\/uploads\\\/2015\\\/08\\\/When_Women_Stopped_Coding___Planet_Money___NPR.png?fit=702%2C514&ssl=1\",\"width\":702,\"height\":514},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/rud.is\\\/b\\\/2015\\\/08\\\/27\\\/coloring-and-drawing-outside-the-lines-in-ggplot\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/rud.is\\\/b\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Coloring (and Drawing) Outside the Lines 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":"Coloring (and Drawing) Outside the Lines 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\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/","og_locale":"en_US","og_type":"article","og_title":"Coloring (and Drawing) Outside the Lines in ggplot - rud.is","og_description":"Time for another Twitter-inspired blog post this week, this time from a tweet by @JonKalodimos: Is there a way to do this in #rstats #ggplot2 https:\/\/t.co\/kxWQFlYpbB &mdash; Jonathan Kalodimos (@JonKalodimos) August 27, 2015 I had seen and appreciated Ann&#8217;s post on her makeover of the main graphic in [NPR&#8217;s story](http:\/\/www.npr.org\/sections\/money\/2014\/10\/21\/357629765\/when-women-stopped-coding) and did a quick mental [&hellip;]","og_url":"https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/","og_site_name":"rud.is","article_published_time":"2015-08-27T14:36:08+00:00","article_modified_time":"2018-03-07T21:43:28+00:00","og_image":[{"url":"https:\/\/rud.is\/b\/wp-content\/uploads\/2015\/08\/When_Women_Stopped_Coding___Planet_Money___NPR.png","type":"","width":"","height":""}],"author":"hrbrmstr","twitter_card":"summary_large_image","twitter_misc":{"Written by":"hrbrmstr","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/#article","isPartOf":{"@id":"https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/"},"author":{"name":"hrbrmstr","@id":"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886"},"headline":"Coloring (and Drawing) Outside the Lines in ggplot","datePublished":"2015-08-27T14:36:08+00:00","dateModified":"2018-03-07T21:43:28+00:00","mainEntityOfPage":{"@id":"https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/"},"wordCount":483,"commentCount":8,"publisher":{"@id":"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886"},"image":{"@id":"https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/#primaryimage"},"thumbnailUrl":"https:\/\/rud.is\/b\/wp-content\/uploads\/2015\/08\/When_Women_Stopped_Coding___Planet_Money___NPR.png","keywords":["post"],"articleSection":["Data Visualization","DataVis","DataViz","ggplot","R"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/","url":"https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/","name":"Coloring (and Drawing) Outside the Lines in ggplot - rud.is","isPartOf":{"@id":"https:\/\/rud.is\/b\/#website"},"primaryImageOfPage":{"@id":"https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/#primaryimage"},"image":{"@id":"https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/#primaryimage"},"thumbnailUrl":"https:\/\/rud.is\/b\/wp-content\/uploads\/2015\/08\/When_Women_Stopped_Coding___Planet_Money___NPR.png","datePublished":"2015-08-27T14:36:08+00:00","dateModified":"2018-03-07T21:43:28+00:00","breadcrumb":{"@id":"https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/#primaryimage","url":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/08\/When_Women_Stopped_Coding___Planet_Money___NPR.png?fit=702%2C514&ssl=1","contentUrl":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/08\/When_Women_Stopped_Coding___Planet_Money___NPR.png?fit=702%2C514&ssl=1","width":702,"height":514},{"@type":"BreadcrumbList","@id":"https:\/\/rud.is\/b\/2015\/08\/27\/coloring-and-drawing-outside-the-lines-in-ggplot\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/rud.is\/b\/"},{"@type":"ListItem","position":2,"name":"Coloring (and Drawing) Outside the Lines 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-WR","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":3442,"url":"https:\/\/rud.is\/b\/2015\/05\/26\/a-quick-incomplete-comparison-of-ggplot2-rbokeh-plotting-idioms\/","url_meta":{"origin":3649,"position":0},"title":"A quick, incomplete comparison of ggplot2 &#038; rbokeh plotting idioms","author":"hrbrmstr","date":"2015-05-26","format":false,"excerpt":"I set aside a small bit of time to give [rbokeh](https:\/\/github.com\/bokeh\/rbokeh) a try and figured I'd share a small bit of code that shows how to make the \"same\" chart in both ggplot2 and rbokeh. #### What is Bokeh\/rbokeh? rbokeh is an [htmlwidget](http:\/\/htmlwidgets.org) wrapper for the [Bokeh](http:\/\/bokeh.pydata.org\/en\/latest\/) visualization library that\u2026","rel":"","context":"In &quot;Charts &amp; Graphs&quot;","block_context":{"text":"Charts &amp; Graphs","link":"https:\/\/rud.is\/b\/category\/charts-graphs\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3538,"url":"https:\/\/rud.is\/b\/2015\/07\/24\/a-path-towards-easier-map-projection-machinations-with-ggplot2\/","url_meta":{"origin":3649,"position":1},"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":3832,"url":"https:\/\/rud.is\/b\/2015\/12\/28\/world-map-panel-plots-with-ggplot2-2-0-ggalt\/","url_meta":{"origin":3649,"position":2},"title":"World Map Panel Plots with ggplot2 2.0 &#038; ggalt","author":"hrbrmstr","date":"2015-12-28","format":false,"excerpt":"James Austin (@awhstin) made some #spiffy 4-panel maps with base R graphics but also posited he didn't use ggplot2 because: \u2026ggplot2 and maps currently do not support world maps at this point, which does not give us a great overall view. That is certainly a box I would not put\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\/12\/facetmaps.png?fit=1154%2C722&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/12\/facetmaps.png?fit=1154%2C722&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/12\/facetmaps.png?fit=1154%2C722&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/12\/facetmaps.png?fit=1154%2C722&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/12\/facetmaps.png?fit=1154%2C722&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":4087,"url":"https:\/\/rud.is\/b\/2016\/03\/12\/ggplot2%e3%81%a7%e5%ad%97%e5%b9%95-subtitles-in-ggplot2\/","url_meta":{"origin":3649,"position":3},"title":"ggplot2\u3067\u5b57\u5e55 [Subtitles in ggplot2]","author":"hrbrmstr","date":"2016-03-12","format":false,"excerpt":"UPDATE: A newer blog post explaining the new ggplot2 additions: http:\/\/rud.is\/b\/2016\/03\/16\/supreme-annotations\/ UPDATE: this capability (+ more) are being rolled into ggplot2-proper. PR will be absorbed into ggplot2 main branch soon. exciting, annotated times ahead! UPDATE: fontsize issue has been fixed & there's a Shiny gadget available for interactively making subtitles.\u2026","rel":"","context":"In &quot;DataVis&quot;","block_context":{"text":"DataVis","link":"https:\/\/rud.is\/b\/category\/datavis-2\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2016\/03\/Fullscreen_3_12_16__7_44_PM.png?fit=1043%2C782&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2016\/03\/Fullscreen_3_12_16__7_44_PM.png?fit=1043%2C782&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2016\/03\/Fullscreen_3_12_16__7_44_PM.png?fit=1043%2C782&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2016\/03\/Fullscreen_3_12_16__7_44_PM.png?fit=1043%2C782&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":12282,"url":"https:\/\/rud.is\/b\/2019\/06\/06\/make-multi-point-dumbbell-plots-in-ggplot2\/","url_meta":{"origin":3649,"position":4},"title":"Make Multi-point &#8220;dumbbell&#8221; Plots in ggplot2","author":"hrbrmstr","date":"2019-06-06","format":false,"excerpt":"A user of the {ggalt} package recently posted a question about how to add points to a geom_dumbbell() plot. For now, this is not something you can do with geom_dumbbell() but with a bit of data wrangling you can do this in a pretty straightforward manner with just your data\u2026","rel":"","context":"In &quot;ggplot&quot;","block_context":{"text":"ggplot","link":"https:\/\/rud.is\/b\/category\/ggplot\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2019\/06\/there-are-three-points-2.png?fit=1200%2C560&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2019\/06\/there-are-three-points-2.png?fit=1200%2C560&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2019\/06\/there-are-three-points-2.png?fit=1200%2C560&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2019\/06\/there-are-three-points-2.png?fit=1200%2C560&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2019\/06\/there-are-three-points-2.png?fit=1200%2C560&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3665,"url":"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/","url_meta":{"origin":3649,"position":5},"title":"Roll Your Own Stats and Geoms in ggplot2 (Part 1: Splines!)","author":"hrbrmstr","date":"2015-09-08","format":false,"excerpt":"A huge change is coming to ggplot2 and you can get a preview of it over at Hadley's github repo. I've been keenly interested in this as I will be fixing, finishing & porting coord_proj to it once it's done. Hadley & Winston have re-built ggplot2 with an entirely new\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":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/posts\/3649","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=3649"}],"version-history":[{"count":0,"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/posts\/3649\/revisions"}],"wp:attachment":[{"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/media?parent=3649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/categories?post=3649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/tags?post=3649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}