本文介绍了我怎样才能在ggplot中绘制一个具有可变bin宽度的直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在 ggplot2 包中找到了函数 dhist(),它实现了由 Denby and Mallows(2009),但我找不到任何使用它的例子。我想用下面的代码来创建变量bin宽度: x1 x2 x x.long< (x,value)= c(x1,x2)) ggplot(x.long,aes(x = value))+ geom_step(aes(x = value ,y = .. density ..,color = variable), stat =bin,binwidth = 0.2)+ coord_cartesian(xlim = c(-1,15)) 我该怎么做? 我交叉发布了 ggplot2 google group 它没有得到回复。如果我在这里得到答案,会在那里发布,反之亦然。 解决方案在这里,感谢哈德利的提示和很多尝试和错误。我还更改了数据库和数量( nbins ),以便更明显地体现效果。 x1 x2 ggplot()+ geom_step(aes(x1,y = .. density ..), stat ='bin',breaks = dhist(x1,nbins = 20), position =dodge,color ='红色')+ geom_step(aes(x2,y = .. density ..), stat ='bin',breaks = dhist(x2,nbins = 20), position = dodge,color ='blue') 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 解决方案 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.8x1 <- 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') 这篇关于我怎样才能在ggplot中绘制一个具有可变bin宽度的直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 06:12