Reputation: 484
Like the person who asked this question (How do I scale the y-axis on a histogram by the x values in R?), I am interested in plotting histograms scaled by a factor other than count.
I really like this solution suggested by one commenter:
d<-rgamma(100,5,1.5)
z<-hist(d,plot=FALSE)
co<-z$counts # original counts of each bin
z$counts<-aggregate(d,list(cut(d,z$breaks)),sum)$x # sum up the value of data in each bin
plot(z)
However, this method does not give the appropriate answer if there are empty bins. For instance:
z2<-hist(d,plot=FALSE,breaks=100)
z2$counts<-aggregate(d,list(cut(d,z2$breaks)),sum)$x
plot(z2)
gives a histogram of clearly the wrong shape. It appears to repeat the non-empty bins as many times as is necessary to fill out the necessary slots.
Is there a better way of doing the same thing?
Upvotes: 1
Views: 261
Reputation: 263471
I think you want to only replace the elements in z2$counts where there are non-zero values:
z2<-hist(d,plot=FALSE,breaks=100)
z2$counts[z2$counts >0] <-aggregate(d,list(cut(d,z2$breaks)),sum)$x
plot(z2, ylab="Totals")
Upvotes: 1