直方图与紧张的地毯

时间:2015-09-08 23:31:13

标签: r ggplot2 histogram

以下脚本

library(ggplot2)
dat<-rnorm(80)
dat<-data.frame(dat)
p<-ggplot(dat, aes(x=dat))+geom_histogram()
p<-p+geom_rug(sides="b", colour="blue")
p

制作了这张漂亮的照片:

Histogram with a rug

但其中许多蓝线重叠。我想添加一些抖动!我尝试过使用:

p<-p+geom_rug(sides="b", position="jitter", colour="blue")

但是我给出了一条错误消息:

  

stat_bin:binwidth默认为范围/ 30。使用&#39; binwidth = x&#39;调整这个。   错误:position_jitter需要以下缺失的美学:y

直方图的y坐标是直方图应自动生成的计数。

如何让我感到紧张?

1 个答案:

答案 0 :(得分:6)

您可以在aes来电中简单地给出0,并且它会将所有内容都绘好:

p + geom_rug(sides = "b", aes(y = 0), position = "jitter", colour = "blue")

使用一些更明显的数据:

dat <- c(rep(1, 50), rep(2, 50))
dat <- data.frame(dat)

没有抖动:

enter image description here

有抖动:

enter image description here