频率分布表和曲线

时间:2014-09-01 11:52:57

标签: r plot

我是R.的新手。我想创建以下数据的频率表和正常频率分布曲线。这样做的最佳方式是什么?

x1= 5,9,5,12,35,55,43,35,18,12,9,22,22,55,7,12
x2= 8,12,24,16,15,20,20,24,35,5,7,9,6,8,6,18,14,12,16,12,5,6,9,10,10,12,18,15,16  

您的建议将不胜感激!

2 个答案:

答案 0 :(得分:1)

尝试:

x1= c(5, 9, 5, 12, 35, 55, 43, 35, 18, 12, 9, 22, 22, 55, 7, 12)                       
x2 = c(8, 12, 24, 16, 15, 20, 20, 24, 35, 5, 7, 9, 6, 8, 6, 18, 14, 12, 16, 12, 5, 6, 9, 10, 10, 12, 18, 15, 16)

library(ggplot2)
ggplot()+geom_density(aes(x2),color='red')+geom_density(aes(x1), color='blue')

enter image description here

(hist(x2))                 # this is the command; rest is output
$breaks                                                                                                                 
[1]  5 10 15 20 25 30 35                                                                                                

$counts                                                                                                                 
[1] 12  7  7  2  0  1                                                                                                   

$density
[1] 0.082758621 0.048275862 0.048275862 0.013793103 0.000000000 0.006896552

$mids
[1]  7.5 12.5 17.5 22.5 27.5 32.5

$xname
[1] "x2"

$equidist
[1] TRUE

attr(,"class")
[1] "histogram"

对于表格版本:

hh = hist(x2)
bb = hh$breaks

hh.breaks = as.character()
for(i in 2:length(bb)) {
    hh.breaks[i-1]=paste0(bb[i-1],'-',bb[i])
}
dd = data.frame(hh.breaks, hh$counts, hh$density, hh$mids)

dd

 hh.breaks hh.counts  hh.density hh.mids
1      5-10        12 0.082758621     7.5
2     10-15         7 0.048275862    12.5
3     15-20         7 0.048275862    17.5
4     20-25         2 0.013793103    22.5
5     25-30         0 0.000000000    27.5
6     30-35         1 0.006896552    32.5

enter image description here

您可能会发现此链接很有用:http://www.statmethods.net/graphs/density.html 如果你只是搜索,还有很多其他的。

答案 1 :(得分:0)

x <- sample(1:10,30,TRUE)
x
 [1] 10  9  2  5  4  9  2 10  2  2  4  5  9  2 10  8  8  6  7  3  8  1  1  7  6  8  6  2  3  5
table(x)
x
 1  2  3  4  5  6  7  8  9 10 
 2  6  2  2  3  3  2  4  3  3 

plot(table(x),type = "l")

enter image description here