我在dhist()
包中找到了函数ggplot2
,它实现了Denby and Mallows (2009)描述的可变宽度直方图,但我找不到它的任何使用示例。我想用下面的代码来创建它
变量箱宽度:
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))
我该怎么做?
注意:我在ggplot2 google group处发布了这个问题。如果我在这里得到答案,将在那里发布,反之亦然
答案 0 :(得分:2)
在这里,感谢哈德利的暗示以及大量的反复试验。我还更改了数据和数量(nbins
),以便效果更明显。
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')
答案 1 :(得分:1)
您可以向geom_step明确提供x
值。
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))