简体   繁体   English

您如何组合数据框列表中的多个箱形图?

[英]How do you combine multiple boxplots from a List of data-frames?

This is a repost from the Statistics portion of the Stack Exchange. 这是来自Stack Exchange统计部分的重新发布。 I had asked the question there, I was advised to ask this question here. 我在那里问过这个问题,建议我在这里问这个问题。 So here it is. 所以这就是。

I have a list of data-frames. 我有一个数据框列表。 Each data-frame has a similar structure. 每个数据帧具有相似的结构。 There is only one column in each data-frame that is numeric. 每个数据框中只有一列数字。 Because of my data-requirements it is essential that each data-frame has different lengths. 由于我的数据要求,每个数据帧的长度必须不同。 I want to create a boxplot of the numerical values, categorized over the attributes in another column. 我想创建一个数值的箱线图,在另一列的属性中进行分类。 But the boxplot should include information from all the data-frames. 但是箱线图应包括来自所有数据帧的信息。

I hope it is a clear question. 我希望这是一个明确的问题。 I will post sample data soon. 我将尽快发布样本数据。

Sam, 山姆,

I'm assuming this is a follow up to this question? 我假设这是问题的后续措施? Maybe your sample data will illustrate the nuances of your needs better (the "categorized over attributes in another column" part), but the same melting approach should work here. 也许您的样本数据将更好地说明您的需求的细微差别(“按另一列的属性分类”部分),但是相同的melting方法应该在这里起作用。

library(ggplot2)
library(reshape2)
#Fake data
a <- data.frame(a = rnorm(10))
b <- data.frame(b = rnorm(100))
c <- data.frame(c = rnorm(1000))

#In a list
myList <- list(a,b,c)


#In a melting pot
df <- melt(myList)

#Separate boxplots for each data.frame
qplot(factor(variable), value, data = df, geom = "boxplot")
#All values plotted together as one boxplot
qplot(factor(1), value, data = df, geom = "boxplot")
a<-data.frame(c(1,2),c("x","y"))
b<-data.frame(c(3,4,5),c("a","b","c"))
boxplot(c(a[1],b[1]))

With the "1"'si select the column i want out of the data-frame. 用“ 1”'si选择我要从数据帧中移出的列。

A data-frames can not have different column-lengths (has to have same number of rows for each column), but you can tell boxplot to plot multiple datasets in parallel. 数据框不能具有不同的列长(每列必须具有相同的行数),但是您可以告诉boxplot并行绘制多个数据集。

Using the melt() function and base R boxplot: 使用melt()函数和基础R箱形图:

#Fake data
a <- data.frame(a = rnorm(10))
b <- data.frame(b = rnorm(100))
c <- data.frame(c = rnorm(100) + 5)

#In a list
myList <- list(a,b,c)

#In a melting pot
df <- melt(myList)

# plot using base R boxplot function
boxplot(value ~ variable, data = df)

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

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