5

Is there a parsimonious way to create a pairs plot that only compares one variable to the many others? In other words, can I plot just one row or column of the standard pairs scatter plot matrix without using a loop?

andrew
  • 3,929
  • 1
  • 25
  • 38

3 Answers3

5

Melt your data then use ggplot with facet.

library("ggplot2")
library("reshape2")

#dummy data
df <- data.frame(x=1:10,
                 a=runif(10),
                 b=runif(10),
                 c=runif(10))

#melt your data
df_melt <- melt(df,"x")

#scatterplot per group
ggplot(df_melt,aes(x,value)) +
  geom_point() +
  facet_grid(.~variable)

enter image description here

zx8754
  • 52,746
  • 12
  • 114
  • 209
5

I'll round it out with a base plotting option (using df from @zx8754):

layout(matrix(seq(ncol(df)-1),nrow=1))
Map(function(x,y) plot(df[c(x,y)]), names(df[1]), names(df[-1]))

yeah

Although arguably this is still a loop using Map.

thelatemail
  • 91,185
  • 12
  • 128
  • 188
3

For the fun, with lattice (with @zx8754 "df_melt"):

library(lattice)

xyplot(value ~ x | variable, data = df_melt, layout = c(3,1), 
       between = list(x=1))

enter image description here