Sum histogram instead of frequency - R

I want to plot a histogram where the y-axis is the sum of the column. I found this example for categorical data: an R histogram that sums, not frequency . However, this is not what I am looking for as it does not apply for continuous data where I would have to define cells.

Let's say I have x and y:

set.seed(1)
mydata <- data.frame(y = runif (100, min= 0, max = 1),
                 x = rpois(100, 15) * 10)

      

A traditional histogram would look like this:

hist (mydata$x)

      

enter image description here

Now how can I get the cumulative sum of y along the y-axis?

+3


source to share


2 answers


This is one way to solve this problem, which uses the hist () function for most of the heavy lifting, and has the advantage that the bar plot of the sum total of y matches the cells and dimensions of the histogram x:

set.seed(1)
mydata <- data.frame(y = runif (100, min= 0, max = 1), x = rpois(100, 15) * 10)
mx <- mydata$x
my <- mydata$y

h <- hist(mydata$x)

breaks <- data.frame(
    "beg"=h$breaks[-length(h$breaks)], 
    "end"=h$breaks[-1]
)

sums <- apply(breaks, MARGIN=1, FUN=function(x) { sum(my[ mx >= x[1] & mx < x[2] ]) })

h$counts <- sums
plot(h, ylab="Sum", main="Sum of y Within x Bins")

      



enter image description here

+3


source


To summarize all the comments, this is what I wanted to have. Thanks @Alex A.

set.seed(1)

mydata <- data.frame(y = runif (100, min= 0, max = 1), x = rpois(100, 15) * 10)

a <- aggregate(mydata$y, by=list(bin=cut(mydata$x, nclass.Sturges(mydata$x))), FUN=sum)
a$bin<- gsub (']','',as.character (a$bin))
a$bin<- gsub (',',' ',as.character (a$bin))

ab2=sapply(strsplit(as.character(a$bin), " "), "[", 2)
barplot(a$x, names.arg=ab2)

      



enter image description here

+3


source







All Articles