将bar转换为hist()函数中的点

时间:2012-07-18 00:03:54

标签: r histogram

hist()将生成由许多“条形”组成的直方图

如何用一个点替换每个条形图,以便将多个点组合在一起看起来像连续曲线?

2 个答案:

答案 0 :(得分:5)

一些虚拟数据

x <- rnorm(50)

# create a histogram
.hist <- hist(x)

# look at the structure to see what is created when calling hist
str(.hist)

## List of 7
## $ breaks     : num [1:10] -2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2
## $ counts     : int [1:9] 1 2 5 6 8 10 10 5 3
## $ intensities: num [1:9] 0.04 0.08 0.2 0.24 0.32 0.4 0.4 0.2 0.12
## $ density    : num [1:9] 0.04 0.08 0.2 0.24 0.32 0.4 0.4 0.2 0.12
## $ mids       : num [1:9] -2.25 -1.75 -1.25 -0.75 -0.25 0.25 0.75 1.25 1.75
## $ xname      : chr "x"
## $ equidist   : logi TRUE
## - attr(*, "class")= chr "histogram"

# we could plot the mids (midpoints) against the counts
with(.hist, plot(mids, counts))

或者你可以简单地使用density

plot(density(x))

enter image description here

答案 1 :(得分:1)

您想要的是直方图的密度图。以下是前两个答案:第一个答案显示了如何在柱状图上叠加密度图。第二个关于如何创建密度图。

Axis-labeling in R histogram and density plots; multiple overlays of density plots

How to Plot Density from Frequency Table