4
votes

I have found the function dhist() in the ggplot2 package that implements the variable width histogram described by Denby and Mallows (2009) but I can not find any examples of its use. I would like to use it with the following code to create variable bin widths:

x1 <- c(rep(0, 250), rlnorm (1000)) 
x2 <- c(rlnorm(1250)) 
x <- data.frame(x1, x2) 
x.long <- melt(x, measure.vars=c("x1","x2")) 
ggplot(x.long, aes(x=value)) + 
 geom_step(aes(x=value, y=..density.., colour=variable), 
   stat="bin", binwidth=0.2) + 
 coord_cartesian(xlim = c(-1, 15)) 

How can I do this?

note: I cross posted this question from the ggplot2 google group where it has been unanswered. If I get an answer here, will post there, and vice versa

2
Perhaps you want to tell us in which package dhist() can be found? - Andrie
@Andrie ggplot2, thanks for pointing out my oversight; I have updated my post - David LeBauer
Which version of ggplot2 are you using? I have 0.8.9 and it isn't there... - Luciano Selzer
@lselzer, I am using version 0.8.8 - David LeBauer
You can use the breaks argument to stat_bin/geom_histogram - hadley

2 Answers

2
votes

Here you go, thanks to a hint from Hadley and a lot of trial and error. I also changed the data and number of bins (nbins) so that the effect would be more noticeable.

library(ggplot2) #using version 0.8.8
x1 <- c(rnorm(100,8,4), rnorm(100, 2,2), rnorm(100,0,10))
x2 <- c(rlnorm(1000),rnorm(1000,1,10), rep(1,500), rep(5,500))
ggplot() + 
  geom_step(aes(x1, y =..density..),
            stat = 'bin',breaks = dhist(x1, nbins =20),
            position = "dodge", color = 'red') +
  geom_step(aes(x2, y =..density..),
            stat = 'bin',breaks = dhist(x2,nbins=20),
            position = "dodge", color = 'blue') 

enter image description here

1
votes

You can explicitly provide x values to geom_step.

t <- seq_len(1250) #your x coords, choose something more interesting
x <- data.frame(t, x1, x2)  #notice 
x.long <- melt(x, measure.vars=c("x1","x2")) 

ggplot(x.long) + 
  geom_step(aes(x=t, y=value, colour=variable)) + 
  coord_cartesian(xlim = c(-1, 15))