2

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))
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Expat_Canuck
  • 113
  • 8
  • Here is an SO question regarding how to make boxplots have variable widths. Perhaps it helps: http://stackoverflow.com/questions/12647774/is-there-an-equivalent-in-ggplot-to-the-varwidth-option-in-plot – lawyeR Apr 27 '15 at 16:49

2 Answers2

3

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))
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
0

Maybe I don't understand well what the question is, but if you are looking for ordering the data frame I think that this could be a good solution:

data2 <- data[ order(cost), ]

Or you can use dplyr package and its arrange function.

SabDeM
  • 7,050
  • 2
  • 25
  • 38
  • As I read it, OP wants the widths of the bars to vary according to abatement, not to order the bars. – lawyeR Apr 27 '15 at 16:47