[英]How to draw all plots of a data frame in R?
I have a data frame representing a benchmark and I would like to produce all possible comparison plots. 我有一个表示基准的数据框,我想生成所有可能的比较图。 Here is a small example of data frame that represents my problem.
这是代表我的问题的数据框的一个小例子。
df = data.frame("A"=c(1,2,3,1,2,3,1,2,3,1,2,3), "B"=c(1,1,1,2,2,2,1,1,1,2,2,2), "C"=c(1,1,1,1,1,1,2,2,2,2,2,2), "D"=c(4,5,6,7,8,9,10,11,12,13,14,15))
I want to produce the following plots. 我要绘制以下图。
D in function of A, when B and C are fixed. 当B和C固定时,D在A函数中起作用。 This would produce four (4) different lines, one for each couple (B,C).
这将产生四(4)条不同的线,每对(B,C)一对。
D in function of B, when A and C are fixed. 当A和C固定时,D在B的作用下。 This would also produce six (6) different lines.
这还将产生六(6)条不同的线。
D in function of C, when A and B are fixed. 当A和B固定时,D在C函数中起作用。 Again, six (6) different lines.
同样,六(6)条不同的线。
Is there a simple way to this in R ? 在R中有简单的方法吗?
For now, I don't mind that they are in different plots or not. 现在,我不介意它们是否位于不同的地块。 Any representation would be ok at this point.
此时任何表示都可以。 I only need all plots to be produced, since I don't know how we want to display our results.
我只需要生成所有图,因为我不知道我们要如何显示结果。
Edit 编辑
I forgot to specify in my example that the columns of the data frame do not have the same factor levels. 我忘了在示例中指定数据框的列不具有相同的因子级别。 Here is a more complete example.
这是一个更完整的示例。
df = data.frame("A"=c(1,2,3,1,2,3,1,2,3,1,2,3),
"B"=c("[0,1]","[0,1]","[0,1]","[1,3]","[1,3]","[1,3]","[0,1]","[0,1]","[0,1]","[1,3]","[1,3]","[1,3]"),
"C"=c(1,1,1,1,1,1,2,2,2,2,2,2),
"D"=c(4,5,6,7,8,9,10,11,12,13,14,15))
Using @mattek's solution, I have the following plots. 使用@mattek的解决方案,我有以下图表。
This is great. 这很棒。 If I could remove the extra values from the x-axis and keep only the corresponding factors for each column, that would be perfect.
如果我可以从x轴上删除多余的值,并只保留每一列的相应因子,那将是完美的。
Possible answer for exploratory analysis that will show correlation between variables and also a smoothing line: 探索性分析的可能答案,它将显示变量之间的相关性以及一条平滑线:
df = data.frame("A"=c(1,2,3,1,2,3,1,2,3,1,2,3), "B"=c(1,1,1,2,2,2,1,1,1,2,2,2), "C"=c(1,1,1,1,1,1,2,2,2,2,2,2), "D"=c(4,5,6,7,8,9,10,11,12,13,14,15))
panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, ...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- cor(x, y)
txt <- format(c(r, 0.123456789), digits = digits)[1]
txt <- paste0(prefix, txt)
if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
text(0.5, 0.5, txt, cex = cex.cor * r)
}
pairs(df, lower.panel = panel.smooth, upper.panel = panel.cor)
library(ggplot2)
library(reshape2)
First, we melt your table: 首先,我们将您的桌子融化:
df.plot = melt(df,
measure.vars = c('A', 'B', 'C'),
id.vars = 'D',
variable.name = 'var.name',
value.name = 'val.abc')
Then, we add groupings column: 然后,我们添加分组列:
df.plot$grouping = rep(1:4, 3, each = 3)
And we are ready to plot: 我们准备绘制:
ggplot(df.plot, aes(x = val.abc, y = D, group = as.factor(grouping))) +
facet_wrap(~ var.name) +
geom_line(aes(colour = var.name)) +
geom_point(aes(colour = var.name))
Using facet_wrap(~ var.name, scale = "free_x")
instead would get rid of non-existant factors in every facet. facet_wrap(~ var.name, scale = "free_x")
会消除每个方面中不存在的因素。
Another option comes from ggplot using the GGaly package: ggplot的另一个选择是使用GGaly软件包:
library(ggplot2)
library(GGally)
this helps a lot if some of your data is a factor, using your data, lets assume that A is a factor variables 如果您的某些数据是一个因素,这会很有帮助,使用您的数据,假设A是一个因素变量
df = data.frame("A"=as.factor(c(1,2,3,1,2,3,1,2,3,1,2,3)), "B"=c(1,1,1,2,2,2,1,1,1,2,2,2), "C"=c(1,1,1,1,1,1,2,2,2,2,2,2), "D"=c(4,5,6,7,8,9,10,11,12,13,14,15))
then ggpairs would make boxplots instead of points, you can choose there 然后ggpairs将使盒图而不是点,您可以在那里选择
Here's what I would do, I would create three new variables which capture the different combinations of A, B, and C fixed: 这就是我要做的,我将创建三个新变量来捕获固定的A,B和C的不同组合:
library(dplyr)
library(ggplot2)
dat <- data.frame("A"=c(1,2,3,1,2,3,1,2,3,1,2,3),
"B"=c(1,1,1,2,2,2,1,1,1,2,2,2),
"C"=c(1,1,1,1,1,1,2,2,2,2,2,2),
"D"=c(4,5,6,7,8,9,10,11,12,13,14,15))
# add variables for A-B, A-C, B-C
dat <- dat %>%
mutate('A - B' = paste(A, '-', B),
'A - C' = paste(A, '-', C),
'B - C' = paste(B, '-', C))
Then we make the plots: 然后我们进行绘图:
ggplot(dat, aes(y = D))+
geom_line(aes(x = C, colour = `A - B`))
ggplot(dat, aes(y = D))+
geom_line(aes(x = B, colour = `A - C`))
ggplot(dat, aes(y = D))+
geom_line(aes(x = A, colour = `B - C`))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.