3

我想为所有可能的变量组合绘制图表。我的代码如下:

 set.seed(12345)
a <- data.frame(Glabel=LETTERS[1:7],   A=rnorm(7, mean = 0, sd = 1),  B=rnorm(7, mean = 0, sd = 1),  C=rnorm(7, mean = 0, sd = 1))
T <- data.frame(Tlabel=LETTERS[11:20], A=rnorm(10, mean = 0, sd = 1), B=rnorm(10, mean = 0, sd = 1), C=rnorm(10, mean = 0, sd = 1))

library(ggplot2)
for(i in 2:(ncol(a)-1))
{
 for(j in (i+1):ncol(a))
 {
  r <- 0.08
  p <- ggplot(data=a, mapping=aes(x=a[, i], y=a[, j])) + geom_point() + theme_bw()
  p <- p + geom_text(data=a, mapping=aes(x=a[, i], y=a[, j], label=Glabel),
                 size=3, vjust=1.35, colour="black")
  p <- p + geom_segment(data = T, aes(xend = T[ ,i], yend=T[ ,j]),
                    x=0, y=0, colour="black",
                    arrow=arrow(angle=25, length=unit(0.25, "cm")))
  p <- p + geom_text(data=T, aes(x=T[ ,i], y=T[ ,j], label=Tlabel), size=3, vjust=0, colour="red")
dev.new()
  print(p)
} 
 }

这段代码工作正常。但是这里使用的方法不推荐(参见@baptiste 评论)并且在功能上不起作用。我想知道完成此任务的最佳和推荐方法是什么。在此先感谢您的帮助。

4

1 回答 1

3

好吧,这是垃圾,但我能做的最好。这是超级低效的,因为它通过每个循环重新创建部分数据lapply。也许别人有更好的东西:

MAT <- outer(names(df)[-1], names(df)[-1], paste)
combs <- sapply(MAT[lower.tri(MAT)], function(x) strsplit(x, " "))
ind <- lapply(combs, function(x) match(x, names(df)))

plotter <- function(cn) { #start junky function
    NAMES <- colnames(df)[cn]
    df2 <- df[cn]
    names(df2)<- c('x1', 'x2')
    p <- ggplot(data=df2, aes(x1, x2)) + geom_point() + theme_bw() +
        scale_x_continuous(name=NAMES[1]) +
        scale_y_continuous(name=NAMES[2])
        dev.new()
        print(p)
} #end of junky function

lapply(ind,  function(x) plotter(cn=x))

编辑:这更好一点:

x <- match(names(df)[-1], names(df))
MAT <- outer(x, x, paste)
combs <- t(sapply(MAT[lower.tri(MAT)], function(x) as.numeric(unlist(strsplit(x, " ")))))

plotter <- function(cn) {
    NAMES <- colnames(df)[cn]
    df2 <- df[cn]
    names(df2)<- c('x1', 'x2')
    p <- ggplot(data=df2, aes(x1, x2)) + geom_point() + theme_bw() +
        scale_x_continuous(name=NAMES[1]) +
        scale_y_continuous(name=NAMES[2])
        dev.new()
        print(p)
}

apply(combs,  1, function(x) plotter(cn=x))
于 2012-04-20T03:48:28.663 回答