我正在尝试在直方图上绘制一条曲线;但是,曲线以某种方式在y = 0处开始,而x在某个负值处开始,但是它需要从x = 0处开始,此时它具有最高的频率。
这些是df['delta'] = df['time'].diff().dt.seconds
的值:
data
[1] 0.41645505 0.17807010 0.04401494 0.00000000 0.53424325 0.00000000 0.78833026 0.14429310 0.00000000 0.35345068 0.00000000 0.00000000
[13] 0.03157549 0.00000000 0.00000000 0.83979615 0.15510495 0.00000000 0.00000000 0.38146542 0.60273251 0.28437203 0.00000000 0.00000000
[25] 0.63672858 0.00000000 0.28479730 0.00000000 0.73017781 0.39795789 0.00000000 0.00000000 0.56448031 0.00000000 0.92790850 0.00000000
[37] 0.00000000 0.46136357 0.27828194 0.00000000 0.01385383 0.36895497 0.06200592 0.00000000 0.17517336 0.57521911 0.00000000 0.32508820
[49] 0.00000000 0.00000000
生成的直方图很好。但是,当我尝试在顶部绘制曲线时:
hist(data)
它产生了一个从(-0.2,0)开始的图,但是数据中没有负数。
我希望在直方图中条形的顶部有一条曲线/线。
答案 0 :(得分:4)
tl; dr 在您的密度声明中使用from=0
来限制范围。 (不要忘记在直方图中使用freq=FALSE
或prob=TRUE
来将直方图缩放为密度而不是计数。)
数据:
dat <- c(0.41645505,0.17807010,0.04401494,0.00000000, 0.53424325,
0.00000000,0.78833026,0.14429310,0.00000000,0.35345068,
0.00000000,0.00000000,0.03157549,0.00000000,0.00000000,
0.83979615,0.15510495,0.00000000,0.00000000,0.38146542,
0.60273251,0.28437203,0.00000000,0.00000000,0.63672858,
0.00000000,0.28479730,0.00000000,0.73017781,0.39795789,
0.00000000,0.00000000,0.56448031,0.00000000,0.92790850,
0.00000000,0.00000000,0.46136357,0.27828194,0.00000000,
0.01385383,0.36895497,0.06200592,0.00000000,0.17517336,
0.57521911,0.00000000,0.32508820,0.00000000,0.00000000)
在from=0
中使用density()
告诉R从0开始输出。如果您想要一条摇摆而不太平滑的行,可以将adjust
参数降低到{{1} }。 @RuiBarradas的答案向您展示了如何通过直方图条的顶部的中点画一条平滑的线-尽管可以说这对于表征密度没有太大的理论意义。
density()
答案 1 :(得分:0)
使用lattice
可以找到并可视化每个bin中的分布:
dat <- c(0.41645505,0.17807010,0.04401494,0.00000000, 0.53424325,
0.00000000,0.78833026,0.14429310,0.00000000,0.35345068,
0.00000000,0.00000000,0.03157549,0.00000000,0.00000000,
0.83979615,0.15510495,0.00000000,0.00000000,0.38146542,
0.60273251,0.28437203,0.00000000,0.00000000,0.63672858,
0.00000000,0.28479730,0.00000000,0.73017781,0.39795789,
0.00000000,0.00000000,0.56448031,0.00000000,0.92790850,
0.00000000,0.00000000,0.46136357,0.27828194,0.00000000,
0.01385383,0.36895497,0.06200592,0.00000000,0.17517336,
0.57521911,0.00000000,0.32508820,0.00000000,0.00000000)
dat.hist <- hist(dat, breaks =6, border = "white", col="gray",main = "")
plot(dat.hist)
library(lattice)
lattice::densityplot( ~ dat | cut(dat, breaks = dat.hist$breaks),
layout = c(5, 1))