

{"id":3665,"date":"2015-09-08T19:06:39","date_gmt":"2015-09-09T00:06:39","guid":{"rendered":"http:\/\/rud.is\/b\/?p=3665"},"modified":"2018-08-27T10:32:23","modified_gmt":"2018-08-27T15:32:23","slug":"roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines","status":"publish","type":"post","link":"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/","title":{"rendered":"Roll Your Own Stats and Geoms in ggplot2 (Part 1: Splines!)"},"content":{"rendered":"<p>A <em>huge<\/em> change is coming to <code>ggplot2<\/code> and you can get a preview of it over at Hadley&#8217;s <a href=\"http:\/\/github.com\/hadley\/ggplot2\">github repo<\/a>. I&#8217;ve been keenly interested in this as I will be fixing, finishing &amp; porting <a href=\"https:\/\/rud.is\/b\/2015\/07\/24\/a-path-towards-easier-map-projection-machinations-with-ggplot2\/\">coord_proj<\/a> to it once it&#8217;s done.<\/p>\n<p>Hadley &amp; Winston have re-built ggplot2 with an entirely new object-oriented system called <code>ggproto<\/code>. With <code>ggproto<\/code> it&#8217;s now possible to easily extend ggplot2 from <em>within your own packages<\/em> (since <code>source()<\/code> is <em>so<\/em> last century), often times with very little effort.<\/p>\n<p>Before attempting to port <code>coord_proj<\/code> I wanted to work through adding a <code>Geom<\/code> and <code>Stat<\/code> since thought it would be cool to be able to have interpolated line charts (and it helps answer some recurring StackOverflow &#8220;spline&#8221;\/ggplot2 questions) and also prefer <code>KernSmooth::bkde<\/code> over the built-in <code>density<\/code> function (which <code>geom_density<\/code> and <code>stat_density<\/code> both use).<\/p>\n<p>To that end, I&#8217;ve made a new github-installable package called <a href=\"http:\/\/github.com\/hrbrmstr\/ggalt\">ggalt<\/a> (h\/t to @jayjacobs for the better package name than I originally came up with) where I&#8217;ll be adding new <code>Geom<\/code>s, <code>Stat<\/code>s, <code>Coord<\/code>s (et al) as I craft them. For now, let me introduce both <code>geom_xspline()<\/code> and <code>geom_bkde()<\/code> to show how easy it is to incorporate new functionality into ggplot2.<\/p>\n<p>While not a requirement, I think it&#8217;s a going to be a good idea to make both a paired <code>Geom<\/code> and <code>Stat<\/code> when adding those types of functionality to ggplot2. I found it easier to work with custom parameters this way and it also makes it <em>feel<\/em> a bit more like the way ggplot2 itself works. For the interpolated line geom\/stat I used R&#8217;s <code>graphics::xpsline<\/code> function. Here&#8217;s all it took to give ggplot2 lines some curves (you can find the commented version <a href=\"https:\/\/github.com\/hrbrmstr\/ggalt\/blob\/master\/R\/geom_xspline.r\">on github<\/a>):<\/p>\n<pre><code class=\"language-r\">geom_xspline <- function(mapping = NULL, data = NULL, stat = \"xspline\",\n                      position = \"identity\", show.legend = NA,\n                      inherit.aes = TRUE, na.rm = TRUE,\n                      spline_shape=-0.25, open=TRUE, rep_ends=TRUE, ...) {\n  layer(\n    geom = GeomXspline,\n    mapping = mapping,\n    data = data,\n    stat = stat,\n    position = position,\n    show.legend = show.legend,\n    inherit.aes = inherit.aes,\n    params = list(spline_shape=spline_shape,\n                  open=open,\n                  rep_ends=rep_ends,\n                  ...)\n  )\n}\n\nGeomXspline <- ggproto(\"GeomXspline\", GeomLine,\n  required_aes = c(\"x\", \"y\"),\n  default_aes = aes(colour = \"black\", size = 0.5, linetype = 1, alpha = NA)\n)\n\nstat_xspline <- function(mapping = NULL, data = NULL, geom = \"line\",\n                     position = \"identity\", show.legend = NA, inherit.aes = TRUE,\n                     spline_shape=-0.25, open=TRUE, rep_ends=TRUE, ...) {\n  layer(\n    stat = StatXspline,\n    data = data,\n    mapping = mapping,\n    geom = geom,\n    position = position,\n    show.legend = show.legend,\n    inherit.aes = inherit.aes,\n    params = list(spline_shape=spline_shape,\n                  open=open,\n                  rep_ends=rep_ends,\n                  ...\n    )\n  )\n}\n\nStatXspline <- ggproto(\"StatXspline\", Stat,\n\n  required_aes = c(\"x\", \"y\"),\n\n  compute_group = function(self, data, scales, params,\n                           spline_shape=-0.25, open=TRUE, rep_ends=TRUE) {\n    tf <- tempfile(fileext=\".png\")\n    png(tf)\n    plot.new()\n    tmp <- xspline(data$x, data$y, spline_shape, open, rep_ends, draw=FALSE, NA, NA)\n    invisible(dev.off())\n    unlink(tf)\n\n    data.frame(x=tmp$x, y=tmp$y)\n  }\n)<\/code><\/pre>\n<p>If that seems like alot of code, it really isn't. What we have there are:<\/p>\n<ul>\n<li>two functions that handle the <code>Geom<\/code> aspects &amp;<\/li>\n<li>two functions that handle the <code>Stat<\/code> aspects.<\/li>\n<\/ul>\n<p>Let's look at the <code>Stat<\/code> functions first, though you can also just <a href=\"https:\/\/github.com\/hrbrmstr\/ggplot2\/blob\/master\/vignettes\/extending-ggplot2.Rmd\">read the handy vignette<\/a>, too.<\/p>\n<h3>Adding <code>Stat<\/code>s<\/h3>\n<p>In this particular case, we have it easy. We get to use <code>geom_line<\/code>\/<code>GeomLine<\/code> as the base <code>geom_<\/code> for the layer since all we're doing is generating more points for it to draw line segments between. We create the creative interface to our new <code>Stat<\/code> with <code>stat_xspline<\/code> add three new parameters with default values:<\/p>\n<ul>\n<li><code>spline_shape<\/code><\/li>\n<li><code>open<\/code><\/li>\n<li><code>rep_ends<\/code><\/li>\n<\/ul>\n<p><em>\"Added three new parameters to what?\"<\/em> you ask? <code>GeomLine<\/code>\/<code>geom_line<\/code> default to <code>StatIdentity<\/code>\/<code>stat_identity<\/code> and if you look at the source code, that <code>Stat<\/code> just returns the data back in the form it came in. We're going to take these three new parameters and pass them to <code>xspline<\/code> and then return entirely new values back for <code>ggplot2<\/code>\/<code>grid<\/code> to draw for us, so we tell it to call our new computation engine by giving it the <code>StatXspline<\/code> value to the layer. By using <code>GeomLine<\/code>\/<code>geom_line<\/code> as the <code>geom<\/code> parameter, all we have to do is ensure we pass back the proper values. We do that in <code>compute_group<\/code> since <code>ggplot2<\/code> will segment the incoming data into groups (via the <code>group<\/code> aesthetic) for us. We take each group and run them through the <code>xspline<\/code> with the parameters the user specified. If I didn't have to use the hack to work around what seems to be errant plot device issues in <code>xspline<\/code>, the call would be one line.<\/p>\n<h3>Adding <code>Geom<\/code>s<\/h3>\n<p>We pair up the <code>Stat<\/code> with a very basic <code>Geom<\/code> \"shim\" so we can use them interchangeably. It's the same idiom, an \"object\" function and the user-callable function. In this case, it's super-lightweight since we're really having <code>geom_line<\/code> do all the work for us. In a [very] future post, I'll cover more complex <code>Geom<\/code>s that require use of the underlying <code>grid<\/code> graphics system, but I suspect most of your own additions may be able to use the lightweight idiom here (and that's covered in the vignette).<\/p>\n<h3>Putting Our New Functions To Work<\/h3>\n<p>With our new additions to <code>ggplot2<\/code>, we can compare the output of <code>geom_smooth<\/code> to <code>geom_xspline<\/code> with some test data:<\/p>\n<pre><code class=\"language-r\">set.seed(1492)\ndat <- data.frame(x=c(1:10, 1:10, 1:10),\n                  y=c(sample(15:30, 10), 2*sample(15:30, 10), 3*sample(15:30, 10)),\n                  group=factor(c(rep(1, 10), rep(2, 10), rep(3, 10)))\n)\n\nggplot(dat, aes(x, y, group=group, color=factor(group))) +\n  geom_point(color=\"black\") +\n  geom_smooth(se=FALSE, linetype=\"dashed\", size=0.5) +\n  geom_xspline(size=0.5)<\/code><\/pre>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3666\" data-permalink=\"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/readme-unnamed-chunk-4-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/09\/README-unnamed-chunk-4-3.png?fit=1344%2C960&amp;ssl=1\" data-orig-size=\"1344,960\" 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=\"README-unnamed-chunk-4-3\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/09\/README-unnamed-chunk-4-3.png?fit=300%2C214&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/09\/README-unnamed-chunk-4-3.png?fit=510%2C364&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/09\/README-unnamed-chunk-4-3.png?resize=510%2C364&#038;ssl=1\" alt=\"README-unnamed-chunk-4-3\" width=\"510\" height=\"364\" class=\"aligncenter size-full wp-image-3666\" \/><\/p>\n<p>The <a href=\"http:\/\/github.com\/hrbrmstr\/ggalt\">github page<\/a> has more examples for the function, but you don't have to be envious of the smooth D3 curves any more.<\/p>\n<p>I realize this particular addition is not extremely helpful\/beneficial, but the next one is. We'll look at adding a new\/more accurate density <code>Stat<\/code>\/<code>Geom<\/code> in the next installment and then discuss the \"on-steroids\" roxygen2 comments you'll end up using for your creations in part 3.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A huge change is coming to ggplot2 and you can get a preview of it over at Hadley&#8217;s github repo. I&#8217;ve been keenly interested in this as I will be fixing, finishing &amp; porting coord_proj to it once it&#8217;s done. Hadley &amp; Winston have re-built ggplot2 with an entirely new object-oriented system called ggproto. With [&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":[678,673,674,753,91],"tags":[810],"class_list":["post-3665","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.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Roll Your Own Stats and Geoms in ggplot2 (Part 1: Splines!) - 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\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Roll Your Own Stats and Geoms in ggplot2 (Part 1: Splines!) - rud.is\" \/>\n<meta property=\"og:description\" content=\"A huge change is coming to ggplot2 and you can get a preview of it over at Hadley&#8217;s github repo. I&#8217;ve been keenly interested in this as I will be fixing, finishing &amp; porting coord_proj to it once it&#8217;s done. Hadley &amp; Winston have re-built ggplot2 with an entirely new object-oriented system called ggproto. With [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/\" \/>\n<meta property=\"og:site_name\" content=\"rud.is\" \/>\n<meta property=\"article:published_time\" content=\"2015-09-09T00:06:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-08-27T15:32:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/rud.is\/b\/wp-content\/uploads\/2015\/09\/README-unnamed-chunk-4-3.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\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/\"},\"author\":{\"name\":\"hrbrmstr\",\"@id\":\"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886\"},\"headline\":\"Roll Your Own Stats and Geoms in ggplot2 (Part 1: Splines!)\",\"datePublished\":\"2015-09-09T00:06:39+00:00\",\"dateModified\":\"2018-08-27T15:32:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/\"},\"wordCount\":713,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886\"},\"image\":{\"@id\":\"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/rud.is\/b\/wp-content\/uploads\/2015\/09\/README-unnamed-chunk-4-3.png\",\"keywords\":[\"post\"],\"articleSection\":[\"Data Visualization\",\"DataVis\",\"DataViz\",\"ggplot\",\"R\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/\",\"url\":\"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/\",\"name\":\"Roll Your Own Stats and Geoms in ggplot2 (Part 1: Splines!) - rud.is\",\"isPartOf\":{\"@id\":\"https:\/\/rud.is\/b\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/rud.is\/b\/wp-content\/uploads\/2015\/09\/README-unnamed-chunk-4-3.png\",\"datePublished\":\"2015-09-09T00:06:39+00:00\",\"dateModified\":\"2018-08-27T15:32:23+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/09\/README-unnamed-chunk-4-3.png?fit=1344%2C960&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/09\/README-unnamed-chunk-4-3.png?fit=1344%2C960&ssl=1\",\"width\":1344,\"height\":960},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/rud.is\/b\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Roll Your Own Stats and Geoms in ggplot2 (Part 1: Splines!)\"}]},{\"@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":"Roll Your Own Stats and Geoms in ggplot2 (Part 1: Splines!) - 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\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/","og_locale":"en_US","og_type":"article","og_title":"Roll Your Own Stats and Geoms in ggplot2 (Part 1: Splines!) - rud.is","og_description":"A huge change is coming to ggplot2 and you can get a preview of it over at Hadley&#8217;s github repo. I&#8217;ve been keenly interested in this as I will be fixing, finishing &amp; porting coord_proj to it once it&#8217;s done. Hadley &amp; Winston have re-built ggplot2 with an entirely new object-oriented system called ggproto. With [&hellip;]","og_url":"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/","og_site_name":"rud.is","article_published_time":"2015-09-09T00:06:39+00:00","article_modified_time":"2018-08-27T15:32:23+00:00","og_image":[{"url":"https:\/\/rud.is\/b\/wp-content\/uploads\/2015\/09\/README-unnamed-chunk-4-3.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\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/#article","isPartOf":{"@id":"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/"},"author":{"name":"hrbrmstr","@id":"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886"},"headline":"Roll Your Own Stats and Geoms in ggplot2 (Part 1: Splines!)","datePublished":"2015-09-09T00:06:39+00:00","dateModified":"2018-08-27T15:32:23+00:00","mainEntityOfPage":{"@id":"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/"},"wordCount":713,"commentCount":4,"publisher":{"@id":"https:\/\/rud.is\/b\/#\/schema\/person\/d7cb7487ab0527447f7fda5c423ff886"},"image":{"@id":"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/#primaryimage"},"thumbnailUrl":"https:\/\/rud.is\/b\/wp-content\/uploads\/2015\/09\/README-unnamed-chunk-4-3.png","keywords":["post"],"articleSection":["Data Visualization","DataVis","DataViz","ggplot","R"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/","url":"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/","name":"Roll Your Own Stats and Geoms in ggplot2 (Part 1: Splines!) - rud.is","isPartOf":{"@id":"https:\/\/rud.is\/b\/#website"},"primaryImageOfPage":{"@id":"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/#primaryimage"},"image":{"@id":"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/#primaryimage"},"thumbnailUrl":"https:\/\/rud.is\/b\/wp-content\/uploads\/2015\/09\/README-unnamed-chunk-4-3.png","datePublished":"2015-09-09T00:06:39+00:00","dateModified":"2018-08-27T15:32:23+00:00","breadcrumb":{"@id":"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/#primaryimage","url":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/09\/README-unnamed-chunk-4-3.png?fit=1344%2C960&ssl=1","contentUrl":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2015\/09\/README-unnamed-chunk-4-3.png?fit=1344%2C960&ssl=1","width":1344,"height":960},{"@type":"BreadcrumbList","@id":"https:\/\/rud.is\/b\/2015\/09\/08\/roll-your-own-stats-and-geoms-in-ggplot2-part-1-splines\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/rud.is\/b\/"},{"@type":"ListItem","position":2,"name":"Roll Your Own Stats and Geoms in ggplot2 (Part 1: Splines!)"}]},{"@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-X7","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":5050,"url":"https:\/\/rud.is\/b\/2017\/02\/15\/ggalt-0-4-0-now-on-cran\/","url_meta":{"origin":3665,"position":0},"title":"ggalt 0.4.0 now on CRAN","author":"hrbrmstr","date":"2017-02-15","format":false,"excerpt":"I'm uncontainably excited to report that the ggplot2 extension package ggalt is now on CRAN. The absolute best part of this package is the R community members who contributed suggestions and new geoms, stats, annotations and integration features. This release would not be possible without the PRs from: Ben Bolker\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\/2017\/02\/RStudio-1.png?fit=1200%2C510&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2017\/02\/RStudio-1.png?fit=1200%2C510&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2017\/02\/RStudio-1.png?fit=1200%2C510&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2017\/02\/RStudio-1.png?fit=1200%2C510&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2017\/02\/RStudio-1.png?fit=1200%2C510&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":3665,"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":3665,"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":3558,"url":"https:\/\/rud.is\/b\/2015\/07\/25\/roll-your-own-gist-comments-notifier-in-r\/","url_meta":{"origin":3665,"position":3},"title":"Roll Your Own Gist Comments Notifier in R","author":"hrbrmstr","date":"2015-07-25","format":false,"excerpt":"As I was putting together the [coord_proj](https:\/\/rud.is\/b\/2015\/07\/24\/a-path-towards-easier-map-projection-machinations-with-ggplot2\/) ggplot2 extension I had posted a (https:\/\/gist.github.com\/hrbrmstr\/363e33f74e2972c93ca7) that I shared on Twitter. Said gist received a comment (several, in fact) and a bunch of us were painfully reminded of the fact that there is no built-in way to receive notifications from said comment\u2026","rel":"","context":"In &quot;R&quot;","block_context":{"text":"R","link":"https:\/\/rud.is\/b\/category\/r\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"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":3665,"position":4},"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":3929,"url":"https:\/\/rud.is\/b\/2016\/02\/11\/plot-the-new-svg-r-logo-with-ggplot2\/","url_meta":{"origin":3665,"position":5},"title":"Plot the new SVG R logo with ggplot2","author":"hrbrmstr","date":"2016-02-11","format":false,"excerpt":"High resolution and SVG versions of the new R logo are finally available. I converted the SVG to WKT (file here) which means we can use it like we would a shapefile in R. That includes plotting! Here's a short example of how to read that WKT and plot the\u2026","rel":"","context":"In &quot;R&quot;","block_context":{"text":"R","link":"https:\/\/rud.is\/b\/category\/r\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2016\/02\/RStudio.png?fit=1198%2C942&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2016\/02\/RStudio.png?fit=1198%2C942&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2016\/02\/RStudio.png?fit=1198%2C942&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2016\/02\/RStudio.png?fit=1198%2C942&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/rud.is\/b\/wp-content\/uploads\/2016\/02\/RStudio.png?fit=1198%2C942&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/posts\/3665","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=3665"}],"version-history":[{"count":0,"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/posts\/3665\/revisions"}],"wp:attachment":[{"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/media?parent=3665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/categories?post=3665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rud.is\/b\/wp-json\/wp\/v2\/tags?post=3665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}