{"id":407,"date":"2014-01-01T22:10:41","date_gmt":"2014-01-02T03:10:41","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=407"},"modified":"2014-01-01T22:10:41","modified_gmt":"2014-01-02T03:10:41","slug":"shading-a-kernel-density-plot-between-two-points","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2014\/01\/01\/shading-a-kernel-density-plot-between-two-points\/","title":{"rendered":"Shading a kernel density plot between two points."},"content":{"rendered":"<div id=\"question\" data-questionid=\"3494593\">\n<table>\n<tbody>\n<tr>\n<td>\n<div itemprop=\"description\">\n<p>I frequently use kernel density plots to illustrate distributions. These are easy and fast to create in R like so:<\/p>\n<pre><code>set.seed(1)\ndraws &lt;- rnorm(100)^2\ndens &lt;- density(draws)\nplot(dens)\n#or in one line like this: plot(density(rnorm(100)^2))<\/code><\/pre>\n<p>Which gives me this nice little PDF:<\/p>\n<p><img decoding=\"async\" alt=\"It's my PDF, not Adobe's\" src=\"http:\/\/www.cerebralmastication.com\/wp-content\/uploads\/2010\/08\/R_pdf.png\" \/><\/p>\n<p>I&#8217;d like to shade the area under the PDF from the 75th to 95th percentiles. It&#8217;s easy to calculate the points using the\u00a0<code>quantile<\/code>\u00a0function:<\/p>\n<pre><code>q75 &lt;- quantile(draws, .75)\nq95 &lt;- quantile(draws, .95)<\/code><\/pre>\n<p>But how do I shade the the area between\u00a0<code>q75<\/code>\u00a0and\u00a0<code>q95<\/code>?<\/p>\n<\/div>\n<div><a title=\"show questions tagged 'r'\" href=\"http:\/\/stackoverflow.com\/questions\/tagged\/r\" rel=\"tag\">\u00a0<\/a><\/div>\n<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div id=\"answers\"><a name=\"tab-top\"><\/a><\/p>\n<div id=\"answers-header\">\n<table>\n<tbody>\n<tr>\n<td><\/td>\n<td>\n<div>\n<p>With the\u00a0<code>polygon()<\/code>\u00a0function, see its help page and I believe we had similar questions here too.<\/p>\n<p>You need to find the index of the quantile values to get the actual\u00a0<code>(x,y)<\/code>\u00a0pairs.<\/p>\n<p><em>Edit:<\/em>\u00a0Here you go:<\/p>\n<pre><code>x1 &lt;- min(which(dens$x &gt;= q75))  \nx2 &lt;- max(which(dens$x &lt;  q95))\nwith(dens, polygon(x=c(x[c(x1,x1:x2,x2)]), y= c(0, y[x1:x2], 0), col=\"gray\"))<\/code><\/pre>\n<p>Output (added by JDL)<\/p>\n<p><img decoding=\"async\" alt=\"alt text\" src=\"http:\/\/www.cerebralmastication.com\/wp-content\/uploads\/2010\/08\/shadedpdf.png\" \/><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div><\/div>\n<p><a name=\"4371473\"><\/a><\/p>\n<div id=\"answer-4371473\" data-answerid=\"4371473\">\n<table>\n<tbody>\n<tr>\n<td><\/td>\n<td>\n<div>\n<p>Another solution:<\/p>\n<pre><code>dd &lt;- with(dens,data.frame(x,y))\nlibrary(ggplot2)\nqplot(x,y,data=dd,geom=\"line\")+\n  geom_ribbon(data=subset(dd,x&gt;q75 &amp; x&lt;q95),aes(ymax=y),ymin=0,\n              fill=\"red\",colour=NA,alpha=0.5)<\/code><\/pre>\n<p>Result:\u00a0<img decoding=\"async\" alt=\"alt text\" src=\"http:\/\/i.stack.imgur.com\/3YvOS.png\" \/><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>\n<div id=\"comments-4371473\" data-localized=\"true\">\n<table style=\"font-family: Tahoma, Verdana, sans-serif;\">\n<tbody>\n<tr>\n<td><\/td>\n<td>\n<div>\n<p>An expanded solution:<\/p>\n<p>If you wanted to shade both tails (copy &amp; paste of Dirk&#8217;s code) and use known x values:<\/p>\n<pre><code>set.seed(1)\ndraws &lt;- rnorm(100)^2\ndens &lt;- density(draws)\nplot(dens)\n\nq2     &lt;- 2\nq65    &lt;- 6.5\nqn08   &lt;- -0.8\nqn02   &lt;- -0.2\n\nx1 &lt;- min(which(dens$x &gt;= q2))  \nx2 &lt;- max(which(dens$x &lt;  q65))\nx3 &lt;- min(which(dens$x &gt;= qn08))  \nx4 &lt;- max(which(dens$x &lt;  qn02))\n\nwith(dens, polygon(x=c(x[c(x1,x1:x2,x2)]), y= c(0, y[x1:x2], 0), col=\"gray\"))\nwith(dens, polygon(x=c(x[c(x3,x3:x4,x4)]), y= c(0, y[x3:x4], 0), col=\"gray\"))<\/code><\/pre>\n<p>Result:<\/p>\n<p><img decoding=\"async\" alt=\"2-tailed poly\" src=\"http:\/\/i.stack.imgur.com\/ONkfd.png\" \/><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><a name=\"7184582\"><\/a><\/p>\n<div id=\"answer-7184582\" data-answerid=\"7184582\">\n<table>\n<tbody>\n<tr>\n<td><\/td>\n<td>\n<div>\n<p>This question needs a\u00a0<code>lattice<\/code>\u00a0answer. Here&#8217;s a very basic one, simply adapting the method employed by Dirk and others:<\/p>\n<pre><code>#Set up the data\nset.seed(1)\ndraws &lt;- rnorm(100)^2\ndens &lt;- density(draws)\n\n#Put in a simple data frame   \nd &lt;- data.frame(x = dens$x, y = dens$y)\n\n#Define a custom panel function;\n# Options like color don't need to be hard coded    \nshadePanel &lt;- function(x,y,shadeLims){\n    panel.lines(x,y)\n    m1 &lt;- min(which(x &gt;= shadeLims[1]))\n    m2 &lt;- max(which(x &lt;= shadeLims[2]))\n    tmp &lt;- data.frame(x1 = x[c(m1,m1:m2,m2)], y1 = c(0,y[m1:m2],0))\n    panel.polygon(tmp$x1,tmp$y1,col = \"blue\")\n}\n\n#Plot\nxyplot(y~x,data = d, panel = shadePanel, shadeLims = c(1,3))<\/code><\/pre>\n<p><img decoding=\"async\" alt=\"enter image description here\" src=\"http:\/\/i.stack.imgur.com\/HnrWm.png\" \/><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I frequently use kernel density plots to illustrate distributions. These are easy and fast to create in R like so: set.seed(1) draws &lt;- rnorm(100)^2 dens&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-407","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=407"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/407\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}