title: R: Histogram with large data set
body: <p><strong>Goal</strong></p>
  
  <p>From the data.frame <code>d</code>, I am trying to make a histogram of the column <code>cMPerSite</code> weighted by <code>bpInPiece</code>. In other words, <code>bpInPiece</code> is the number of observations at each <code>cMPerSite</code> value.</p>
  
  <p>The Y-axis should represent densities and the X-axis should be on a log scale.</p>
  
  <p><strong>Attempts</strong></p>
  
  <p>I could do something like (which could be improved by pre-allocating the memory size for <code>x</code>).</p>
  
  <pre><code>x = c()
  for (row in 1:nrow(d))
  {
      x = c(x, rep(d$cMPerSite[row],d$bpInPiece[row]))
  }
  hist(x,breaks=100, freq=FALSE)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/pe1xg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pe1xg.png" alt="enter image description here"></a></p>
  
  <p>but this becomes completely impractical when there is too much data (I have about 10 millions rows in my full data set) because <code>x</code> becomes too large to be stored in the RAM. Also, putting the X-axis in log scale is, I think, necessarily a bit of a mess.</p>
  
  <p>Alternatively, I would have thought I could do </p>
  
  <pre><code>ggplot(d) + geom_histogram(aes(x = cMPerSite, y=bpInPiece), stat="identity") + scale_x_log10() + theme_classic(25)
  Warning: Ignoring unknown parameters: binwidth, bins, pad
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/kUqsE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kUqsE.png" alt="enter image description here"></a></p>
  
  <p>but, for some reason I do not understand, nothing gets displayed. Also, I am not sure how to put the Y-axis in density rather than count.</p>
  
  <p>I suppose the bin size should vary logarithmically as the X-axis varies but that's confuses me as it would result in bins gathering an "artificial" high number of observations. Not sure how histograms are typically displayed with log scale X axis. Note that <code>ggplot(d) + geom_histogram(aes(x = cMPerSite, y=bpInPiece), stat="identity")</code> does not display anything either so the problem is not only a question of log scale on the X-axis.</p>
  
  <p><strong>Can you help me to make this histogram?</strong></p>
  
  <p><strong>Subset of my data</strong></p>
  
  <pre><code>structure(list(chrom = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), end = c(241608, 
  612298, 715797, 956634, 983330, 1190613, 1236417, 1330208, 1391915, 
  1464000, 1911436, 1913462, 2092038, 2169783, 2354812, 2363639, 
  2544241, 2551672, 2575287, 2589721, 2659117, 2884565, 3037319, 
  3100967, 3152276, 4319658, 4335072, 6301896, 6550219, 6596684, 
  7132319, 7435267, 7469158, 7604030, 7937619, 8131876, 9359659, 
  9598491, 9945959, 10262757, 10392172, 10646861, 10816847, 11094415, 
  11360199, 11964985, 12220179, 12222166, 12389943), cMInPiece = c(0, 
  1e-07, 1e-07, 0.7118558, 9.99999999473644e-08, 0.9540829, 9.99999998363421e-08, 
  0.4967211, 1.244988, 0.2137991, 8.808171, 0.500545200000001, 
  1.5721302, 1.6856566, 2.2552469, 1.0000000116861e-07, 2.6973586, 
  0.355113100000001, 0.355233800000001, 1.0000000116861e-07, 1.4903822, 
  2.8174978, 1.0000000116861e-07, 0.355231, 1.0000000116861e-07, 
  8.2735924, 0.425817699999996, 6.4568106, 0.372779399999999, 0.363684999999997, 
  0.181640399999999, 0.177473599999999, 1.0000000116861e-07, 0.177463800000005, 
  0.355294099999995, 1.0000000116861e-07, 1.6101482, 1.0000000116861e-07, 
  0.533477099999999, 0.355287800000006, 9.99999940631824e-08, 1.0000000116861e-07, 
  1.0000000116861e-07, 1.0000000116861e-07, 1.0000000116861e-07, 
  1.0000000116861e-07, 9.99999940631824e-08, 1.0000000116861e-07, 
  1.0000000116861e-07), bpInPiece = c(241608, 370690, 103499, 240837, 
  26696, 207283, 45804, 93791, 61707, 72085, 447436, 2026, 178576, 
  77745, 185029, 8827, 180602, 7431, 23615, 14434, 69396, 225448, 
  152754, 63648, 51309, 1167382, 15414, 1966824, 248323, 46465, 
  535635, 302948, 33891, 134872, 333589, 194257, 1227783, 238832, 
  347468, 316798, 129415, 254689, 169986, 277568, 265784, 604786, 
  255194, 1987, 167777), cMPerSite = c(1e-16, 2.69767190914241e-13, 
  9.66192910076426e-13, 2.95575762860358e-06, 3.74587953054257e-12, 
  4.60280341369046e-06, 2.18321543612659e-12, 5.29604226418313e-06, 
  2.01757985317711e-05, 2.96593049871679e-06, 1.96858790977928e-05, 
  0.000247060809476802, 8.80370374518411e-06, 2.16818650717088e-05, 
  1.21886131363192e-05, 1.13288774406491e-11, 1.49353750235324e-05, 
  4.77880635176962e-05, 1.50427186110523e-05, 6.92808654348135e-12, 
  2.14764856764078e-05, 1.24973288740641e-05, 6.54647349127419e-13, 
  5.58118086978381e-06, 1.94897583598608e-12, 7.08730509807415e-06, 
  2.76253860127155e-05, 3.28286140498591e-06, 1.50118756619403e-06, 
  7.82707414182711e-06, 3.39112268615754e-07, 5.85821989252278e-07, 
  2.95063589650969e-12, 1.31579423453352e-06, 1.06506539484214e-06, 
  5.14781970114898e-13, 1.31142734506016e-06, 4.18704366117646e-13, 
  1.53532728193675e-06, 1.1214963478305e-06, 7.72707909154135e-13, 
  3.92635728942395e-13, 5.88283747888707e-13, 3.60272081683082e-13, 
  3.76245376578762e-13, 1.65347744770232e-13, 3.91858719496471e-13, 
  5.03271269092148e-11, 5.96029260080999e-13)), .Names = c("chrom", 
  "end", "cMInPiece", "bpInPiece", "cMPerSite"), row.names = c(NA, 
  -49L), class = "data.frame")
  </code></pre>