首页 > 解决方案 > 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!

标签: rplotprobability-densitykernel-densitydensity-plot

解决方案


You should be sure to include a reproducible example in the future so I can be sure I'm answering the right question, but the lattice library should get you close:

library(stats)
library(lattice)
densityplot( ~ height | voice.part, data = singer, layout = c(2, 4),  
             xlab = "Height (inches)", bw = 5)

enter image description here


推荐阅读