2012-09-20 27 views
4

嗨我试图在ggplot中绘制直方图,但我的数据并没有所有的值,但值和发生次数。R如何bin加权数据

value=c(1,2,3,4,5,6,7,8,9,10) 
weight<-c(8976,10857,10770,14075,18075,20757,24770,14556,11235,8042) 
df <- data.frame(value,weight) 
df 
    value weight 
1  1 8976 
2  2 10857 
3  3 10770 
4  4 14075 
5  5 18075 
6  6 20757 
7  7 24770 
8  8 14556 
9  9 11235 
10 10 8042 

人会知道无论怎么仓值或如何绘制分级值的直方图。
我想要得到的东西,看起来像

bin weight 
1 1-2 19833 
2 3-4 24845 
... 

回答

0

只需扩展您的数据:

value=c(1,2,3,4,5,6,7,8,9,10) 
weight<-c(8976,10857,10770,14075,18075,20757,24770,14556,11235,8042) 
dat = rep(value,weight) 
# plot result 
histres = hist(dat) 

而且histres包含一些可能有用的信息,如果你想直方图数据的详细信息。

2

这里有长达二进制化数据的方法之一:

df$bin <- findInterval(df$value,seq(1,max(df$value),2)) 
result <- aggregate(df["weight"],df["bin"],sum) 
# get your named bins automatically without specifying them individually 
result$bin <- tapply(df$value,df$bin,function(x) paste0(x,collapse="-")) 

# result 
    bin weight 
1 1-2 19833 
2 3-4 24845 
3 5-6 38832 
4 7-8 39326 
5 9-10 19277 

# barplot it (base example since Roman has covered ggplot) 
with(result,barplot(weight,names.arg=bin)) 
1

我想补充另一个变量指定分级,然后

df$group <- rep(c("1-2", "3-4", "5-6", "7-8", "9-10"), each = 2) 

使用ggplot绘制。

ggplot(df, aes(y = weight, x = group)) + stat_summary(fun.y="sum", geom="bar") 

enter image description here