How do I generate a histogram for each column of my table?

后端 未结 3 1171
执念已碎
执念已碎 2020-12-14 10:05

I have a table of data with a column representing a lab value for each study subject (rows).

I want to generate a series of histograms showing the distribution of v

3条回答
  •  长情又很酷
    2020-12-14 10:53

    You could generate the plots in a for loop with something like this, if your data frame is named "df" and you want to generate histograms starting with column 2 (if column 1 is your id):

    for (col in 2:ncol(df)) {
        hist(df[,col])
    }
    

    The hist function automatically calculates a reasonable bin width, or you can specify a fixed number of bins for all histograms, by adding the breaks argument:

    hist(df[,col], breaks=10)
    

    If you use RStudio, all your plots will be automatically be saved in the plots pane. If not, you will need to save each plot to a separate file inside the loop, as explained here: http://www.r-bloggers.com/automatically-save-your-plots-to-a-folder/

提交回复
热议问题