Search code examples
rplotprobability-densitykernel-densitydensity-plot

R: How do I plot multiple density plots on one chart, with columns pulled from excel


total R/programming newbie here. I'm trying to grab column data (multiple columns) from an excel sheet, create a density plot for each of those columns, and then plot them onto one chart and make it look nice.

The excel data are just columns (colA, colB, colC, etc) with numerical values in them (about 2000 data points per column of data). I've managed to figure out how to plot one column:

library(readxl)
setwd("directoryLocation")
dft <- read_excel("sample.xlsx", sheet="columns")
d <- density(dft$`colA`)
plot(d)

but I'm at a loss on incorporating all the columns. Any ideas on how to proceed (or a better method) would be greatly appreciated. Cheers!


Solution

  • Thanks for the advice; lattice looks like another great method! Sorry for not being clear about it, here is what I was trying to do and how I solved it:

    1. The data (columns inside an excel spreadsheet...about 2000 lines per column):
    • colA colB colC ...
    • 34.4 65.5 55.1 ...
    • 34.6 64.5 54.6 ...
    1. What I wanted to do was pull the data in these excel spreadsheet columns and make a density plot for each column, but using the same axes (so one chart, multiple plots)

    2. Here's how I made it work:

    NOTE: I realized that the first cell in each column was a "header" and I could just call those header names from the dataframe into each new plotline, using

    plot(density(dataFrameName$headerName)) for the first one, and then lines(density(dataFrameName$headerName)) for each additional data column.

    library(readxl)
    setwd("pathToWorkingDir")
    
    dft <- read_excel("excelSpreadsheet.xlsx", sheet="sheetName")
    
    tiff("createPlotImage.tiff", height = 8, width = 8, units = 'in', res=300)
    plot(density(dft$`colA`), panel.first = abline(h=0,col="black"), xlim = c(-60,-45), ylim =c(0,1.5), lwd=3, col='#FF0000', main = "", cex.axis = 1.4, ann=FALSE)
    lines(density(dft$`colB`), lwd=3,col='#FF9900')
    lines(density(dft$`colC`), lwd=3,col='#FFFF00')
    legend('topleft',bty="n",c('colA','colB','colC'), ncol=1, col=c('#FF0000','#FF9900','#FFFF00','#66FF66'),text.width = 6, lwd = 3,cex = 1)
    
    dev.off()
    

    The result: plot of the code