简体   繁体   English

R-数据帧中数据的直方图/密度图

[英]R - Histogram / density plot of data in data frame

I'm sure there is an obvious and easy solution for this ... but I'm drawing a complete blank on this one: how can I create a histogram and/or density plot using ALL data points in a data set that consists of several rows and columns? 我敢肯定有一个明显且简单的解决方案...但是我要在此问题上画一个完整的空白:如何使用包含以下内容的数据集中的所有数据点创建直方图和/或密度图几行和几列?

Example data: 示例数据:

MYdata <- data.frame(Name = round(rnorm(12, 1000, 250)), 
                     Sample1 = rnorm(12, 10000, 2500), 
                     Sample2 = rnorm(12, 10000, 2500), 
                     Sample3 = rnorm(12, 10000, 2500), 
                     Sample4 = rnorm(12, 10000, 2500)) 

"Name" is the header of the column containing the row names. “名称”是包含行名称的列的标题。 Columns Sample1 to Sample4 contain the data. Sample1到Sample4列包含数据。 hist() or geom_histogram() require an x = . hist()geom_histogram()需要x =。 But I don't know what x is in my data set. 但是我不知道数据集中的x是多少。

Thanks for your help. 谢谢你的帮助。

Check out melt from the reshape2 package. 检查reshape2包装中的melt Since Hadley made them both reshape2 , plyr and ggplot they play very nicely together: 自从Hadley使它们同时reshape2plyrggplot他们一起玩得非常好:

MYdata.melt <- melt(MYdata, id.vars='Name')

ggplot(MYdata.melt, aes(x=value, color=variable)) + geom_density()

I used geom_density() since your data is continuous, but changing to geom_histogram() is straight forward. 我使用了geom_density()因为您的数据是连续的,但直接更改为geom_histogram()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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