Title: A function to add a colored polygon to a plot for confidence intervals · Issue #2 · dapperstats/bbplot · GitHub
Open Graph Title: A function to add a colored polygon to a plot for confidence intervals · Issue #2 · dapperstats/bbplot
X Title: A function to add a colored polygon to a plot for confidence intervals · Issue #2 · dapperstats/bbplot
Description: The title says it all. The function, which I called ribbon() is a slight modification to polygon() with quality of life improvements to make it easier to add colored confidence intervals to a plot. Default values are taken for all polygo...
Open Graph Description: The title says it all. The function, which I called ribbon() is a slight modification to polygon() with quality of life improvements to make it easier to add colored confidence intervals to a plot....
X Description: The title says it all. The function, which I called ribbon() is a slight modification to polygon() with quality of life improvements to make it easier to add colored confidence intervals to a plot....
Opengraph URL: https://github.com/dapperstats/bbplot/issues/2
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"A function to add a colored polygon to a plot for confidence intervals","articleBody":"The title says it all. The function, which I called `ribbon()` is a slight modification to `polygon()` with quality of life improvements to make it easier to add colored confidence intervals to a plot. Default values are taken for all polygon arguments, except for border, which is set to NA. Additionally, an alpha argument has been added to accommodate partially transparent confidence intervals. Any of the input values can be overwritten within this function's call.\r\n\r\nA standard workflow would look something like this, which could not add in transparency. \r\n\r\n```R\r\ndata(cars)\r\n# fit model\r\nm1 \u003c- lm(\r\n dist ~ speed,\r\n data = cars\r\n)\r\n# make predictions\r\npreds \u003c- predict(\r\n m1, \r\n newdata = data.frame(speed = 10:25),\r\n interval = \"confidence\"\r\n)\r\n# base plot\r\nblank(\r\n xlim = c(10,25),\r\n ylim = c(15,120),\r\n xlab = \"Speed\",\r\n ylab = \"Stopping distance\",\r\n xaxt = \"s\",\r\n yaxt = \"s\",\r\n bty = \"l\",\r\n las = 1\r\n)\r\npolygon(\r\n x = c(10:25, rev(10:25)),\r\n y = c(preds[,2], rev(preds[,3])),\r\n col = \"purple\",\r\n border = NA\r\n)\r\n```\r\n\r\nThe function I am suggesting would replace polygon and in this case would look like:\r\n```R\r\nribbon(\r\n x=10:25,\r\n y=preds[,c(\"lwr\",\"upr\")],\r\n col = \"purple\",\r\n alpha = 0.5\r\n)\r\n\r\n```\r\nIn this case x does not need to be reversed (i.e., `x = c(10:25, rev(10:25))`) and you can input a two-column data.frame or matrix into `y`. Likewise, there is an `alpha` argument now which uses base R to set the alpha channel \u0026 make colors transparent if you want them to be. Code for this function is:\r\n\r\n```R\r\nribbon \u003c- function(x, y, density=NULL, angle=45, border=NA,\r\n col=NA, lty= par(\"lty\"),...,fillOddEven=FALSE,\r\n alpha = NULL\r\n ){\r\n # error checks\r\n if(!any(is.na(col)) \u0026 length(col)\u003e1){\r\n warning(\"Two values input to col. Only first element used.\")\r\n col \u003c- col[1]\r\n }\r\n # check if y is a matrix\r\n if(is.matrix(y)|is.data.frame(y)){\r\n y \u003c- c(y[,1], rev(y[,2]))\r\n # check if x is half the length of y\r\n if(length(y)/length(x) == 2){\r\n x \u003c- c(x, rev(x))\r\n }\r\n }\r\n # evaluate color and alpha channel\r\n if( is.na(col) ){\r\n my_col \u003c- NA\r\n } else { # otherwise go through color process\r\n # \r\n if(\r\n length(grep(\"^#\", col)) == 1 \u0026 # if start with hash\r\n nchar(col)\u003e7 # \u0026 alpha channel is present\r\n ){\r\n if(!is.null(alpha)){\r\n warning(\"col already has alpha channel, ignoring alpha argument.\")\r\n my_col \u003c- col\r\n } else {\r\n my_col \u003c- col\r\n }\r\n } else {\r\n # get rgb\r\n my_rgbs \u003c- col2rgb(col)\r\n # set color\r\n my_col \u003c- rgb(\r\n my_rgbs[1],my_rgbs[2],my_rgbs[3],max = 255,alpha = 255 * alpha\r\n )\r\n } \r\n }\r\n polygon(\r\n x = x, y = y, density = density, angle = angle,\r\n border = border, col = my_col, lty = lty,\r\n fillOddEven = fillOddEven, ...\r\n )\r\n}\r\n```\r\n\r\nCode, documentation, and tests are written. Happy to submit a PR if you want to check it out!","author":{"url":"https://github.com/mfidino","@type":"Person","name":"mfidino"},"datePublished":"2021-09-02T19:57:03.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/2/bbplot/issues/2"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:41471370-5d34-259f-bba5-19bbc26dcebe |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A01E:2B524B:2F468:419CD:69810380 |
| html-safe-nonce | e18f7370c16213d5fd111774a2f52707562278b99776fab318430738e7182c1e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMDFFOjJCNTI0QjoyRjQ2ODo0MTlDRDo2OTgxMDM4MCIsInZpc2l0b3JfaWQiOiI1MjQ5NDQ3MTE4NDM0Nzk2NDE2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | fb6d113852278fd10cc3e7641de9b24d3a1419f539e9d2ec22f6820e76e3f2e4 |
| hovercard-subject-tag | issue:987107924 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/dapperstats/bbplot/2/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a581fecc79124d939f301537177a5e79244d24deb781abd86de25437f3fbc694/dapperstats/bbplot/issues/2 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a581fecc79124d939f301537177a5e79244d24deb781abd86de25437f3fbc694/dapperstats/bbplot/issues/2 |
| og:image:alt | The title says it all. The function, which I called ribbon() is a slight modification to polygon() with quality of life improvements to make it easier to add colored confidence intervals to a plot.... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mfidino |
| hostname | github.com |
| expected-hostname | github.com |
| None | 39fe8101494cbb823c09b619b68c80cd4d05ab7279997038dbe06bb91608abe1 |
| turbo-cache-control | no-preview |
| go-import | github.com/dapperstats/bbplot git https://github.com/dapperstats/bbplot.git |
| octolytics-dimension-user_id | 41246652 |
| octolytics-dimension-user_login | dapperstats |
| octolytics-dimension-repository_id | 191252615 |
| octolytics-dimension-repository_nwo | dapperstats/bbplot |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 191252615 |
| octolytics-dimension-repository_network_root_nwo | dapperstats/bbplot |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d5b34a4e4898b066c629879feb4b184bc471d6a7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width