简体   繁体   中英

Better method to scale the y-axis on a histogram by the x values in R?

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?

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM