Search code examples
rhistogrambins

histogram with varying bin widths


I am trying to replicate the concept of chart Fig 1 from the following paper (http://dx.doi.org/10.1016/j.envsci.2011.08.004):

enter image description here

It is a histogram whose bin widths vary dependent upon the value of x and whose height depends on variable y. The precise values in the chart are not of concern - rather, understanding how to reproduce it.

The following code creates a data frame with two characteristics (abatement and cost) for each measure. the width of measure is the abatement, and the height of measure is cost. The measure should be ordered from least cost to highest cost.

measure <- c(LETTERS)
abatement <- c(sample(1:100, 26))
cost <- c(sample(-100:250, 26))
data <- data.frame(cbind(measure, abatement, cost))

Solution

  • Technically speaking, this is a barplot and not a histogram (histograms specifically refer to barplots used to represent binned frequencies of continuous variables) ...

    Your cbind() is messing things up (converting abatement and cost to factors):

    data <- data.frame(measure, abatement, cost)
    

    Here's a start:

    with(dplyr::arrange(data,cost),
         barplot(width=abatement,height=cost,space=0))