开发者

How can I plot a histogram with variable bin widths in ggplot?

I have found the function dhist() in the ggplot2 package that implements the variable width histogram described by Denby and Mallows (2009)开发者_如何学C 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


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') 

How can I plot a histogram with variable bin widths in ggplot?


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))     
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜