Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a hack using <code>ggplot_build</code>. The idea is to first get your old/original plot:</p> <pre><code>p &lt;- ggplot(data = X, aes(x=C)) + geom_histogram() </code></pre> <p>stored in <code>p</code>. Then, use <code>ggplot_build(p)$data[[1]]</code> to extract the data, specifically, the columns <code>xmin</code> and <code>xmax</code> (to get the same breaks/binwidths of histogram) and <code>count</code> column (to normalize the percentage by <code>count</code>. Here's the code:</p> <pre><code># get old plot p &lt;- ggplot(data = X, aes(x=C)) + geom_histogram() # get data of old plot: cols = count, xmin and xmax d &lt;- ggplot_build(p)$data[[1]][c("count", "xmin", "xmax")] # add a id colum for ddply d$id &lt;- seq(nrow(d)) </code></pre> <p>How to generate data now? What I understand from your post is this. Take for example the first bar in your plot. It has a count of 2 and it extends from <code>xmin = 147</code> to <code>xmax = 156.8</code>. When we check <code>X</code> for these values:</p> <pre><code>X[X$C &gt;= 147 &amp; X$C &lt;= 156.8, ] # count = 2 as shown below # C1 C2 C # 19 91 63 154 # 75 86 70 156 </code></pre> <p>Here, I compute <code>(91+86)/(154+156)*(count=2) = 1.141935</code> and <code>(63+70)/(154+156) * (count=2) = 0.8580645</code> as the two normalised values for each bar we'll generate.</p> <pre><code>require(plyr) dd &lt;- ddply(d, .(id), function(x) { t &lt;- X[X$C &gt;= x$xmin &amp; X$C &lt;= x$xmax, ] if(nrow(t) == 0) return(c(0,0)) p &lt;- colSums(t)[1:2]/colSums(t)[3] * x$count }) # then, it just normal plotting require(reshape2) dd &lt;- melt(dd, id.var="id") ggplot(data = dd, aes(x=id, y=value)) + geom_bar(aes(fill=variable), stat="identity", group=1) </code></pre> <p>And this is the original plot:</p> <p><img src="https://i.stack.imgur.com/d6aer.png" alt="original_ggplot2_plot"></p> <p>And this is what I get:</p> <p><img src="https://i.stack.imgur.com/WTS3I.png" alt="ggplot2_weird_histogram_plot"></p> <p><strong>Edit:</strong> If you also want to get the breaks proper, then, you can get the corresponding <code>x</code> coordinates from the old plot and use it here instead of <code>id</code>:</p> <pre><code>p &lt;- ggplot(data = X, aes(x=C)) + geom_histogram() d &lt;- ggplot_build(p)$data[[1]][c("count", "x", "xmin", "xmax")] d$id &lt;- seq(nrow(d)) require(plyr) dd &lt;- ddply(d, .(id), function(x) { t &lt;- X[X$C &gt;= x$xmin &amp; X$C &lt;= x$xmax, ] if(nrow(t) == 0) return(c(x$x,0,0)) p &lt;- c(x=x$x, colSums(t)[1:2]/colSums(t)[3] * x$count) }) require(reshape2) dd.m &lt;- melt(dd, id.var="V1", measure.var=c("V2", "V3")) ggplot(data = dd.m, aes(x=V1, y=value)) + geom_bar(aes(fill=variable), stat="identity", group=1) </code></pre> <p><img src="https://i.stack.imgur.com/ap7zP.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload