3

I want to plot graphs for all possible combinations of variables. My code is below:

 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)
} 
 }

This code works fine. But the method used here is not recommended (See @baptiste comment) and does not work in function. I want to know what is the best and recommended way to accomplish this task. Thanks in advance for your help.

Community
  • 1
  • 1
MYaseen208
  • 22,666
  • 37
  • 165
  • 309
  • 2
    Have you looked at ggpairs from the GGally package? – joran Apr 20 '12 at 02:57
  • @Joran that's good thinking but this question it self is pretty interesting. I can get close rather easily but ggplot requires you have to supply an object of the data set and I can't with out inefficiently making a fake data set inside the function call each time I lapply it. – Tyler Rinker Apr 20 '12 at 02:59
  • 1
    You can probably use my response below and put the stuff you added in. Please don't change stuff unless you're saying you edited in the thread as then future searchers won't understand why answers look nothing like the "original" question. – Tyler Rinker Apr 20 '12 at 03:57

1 Answers1

3

Alright this is garbage but the best I could do. It's super inefficient as it recreates a partial data with each loop through lapply. Maybe someone else has something better:

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))

EDIT: This is a bit better:

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))
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519