R as weighted data in the hopper

Hi, I am trying to draw a histogram in ggplot, but my data does not have all the values, but the values ​​and the number of occurrences.

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 

Does anyone know how to align values ​​or how to plot a histogram of binn values.
I want to get what will look like

  bin weight 1 1-2 19833 2 3-4 24845 ... 
+4
source share
3 answers

Here is one method to add data up:

 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)) 
+2
source

I would add another variable that stands for binning, and then

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

draw it with ggplot.

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

enter image description here

+1
source

Just expand your data:

 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) 

And histres contains some potentially useful information if you need details of the histogram data.

0
source

Source: https://habr.com/ru/post/1435268/


All Articles