Skip to content

2 matrix scatter x [a lot]

2 messages · Ben qant, Dennis Murphy

#
Hi:

Here's one way, using the following reproducible example.

# Method 1: the variable names are the same in each data frame
# Create two separate data frames
ds1 <- data.frame(x1 = rnorm(10), x2 = rnorm(10), x3 = rnorm(10))
ds2 <- data.frame(x1 = rnorm(10), x2 = rnorm(10), x3 = rnorm(10))

# Melt the data to create a factor whose levels are the variable
# names and a variable 'value' to contain the corresponding values
library('reshape')
dm1 <- melt(ds1)
# Since the values are different in each data frame, change the
# name of the value variable in each
names(dm1)[2] <- 'val1'
dm2 <- melt(ds2)
names(dm2)[2] <- 'val2'
# Since I know the two melted data frames have the same
# dimensions, I can cbind the value variable of the second
# to the first
dm <- cbind(dm1, val2 = dm2[['val2']])

# Conditioning plots:
library('lattice')
library('ggplot2')

# lattice version
xyplot(val2 ~ val1 | variable, data = dm)
# ggplot2 version
ggplot(dm, aes(x = val1, y = val2)) + geom_point() +
   facet_wrap( ~ variable)

# Method 2: Variable names are different
ds1 <- data.frame(x1 = rnorm(10), x2 = rnorm(10), x3 = rnorm(10))
ds2 <- data.frame(y1 = rnorm(10), y2 = rnorm(10), y3 = rnorm(10))

dm1 <- melt(ds1)
names(dm1)[2] <- 'val1'
dm2 <- melt(ds2)
names(dm2)[2] <- 'val2'
dm <- cbind(dm1, val2 = dm2[['val2']])
# Change the level labels of variable to represent the
# column numbers instead:
dm$Variable <- factor(dm$variable,
                 labels = seq_len(length(levels(dm$variable))))

xyplot(val2 ~ val1 | Variable, data = dm, xlab = 'x', ylab = 'y')
ggplot(dm, aes(x = val1, y = val2)) + geom_point() +
   facet_wrap( ~ Variable) + labs(x = 'x', y = 'y')

You've probably got something more complicated than this in terms of
variable names, but the outline above should be enough to get you
started.

HTH,
Dennis
On Mon, Aug 15, 2011 at 3:13 PM, Ben qant <ccquant at gmail.com> wrote: