0

I have one predictor variable, Y, and many Xs. I would like to plot all of the Xs against the Y, how you would using pairs(), but I do not want to see the matrix of every combination.

I just want to see one Y row and X columns or one Y column and X rows. Any help would be appreciated.

zx8754
  • 52,746
  • 12
  • 114
  • 209
runningbirds
  • 6,235
  • 13
  • 55
  • 94

2 Answers2

3

Let's say your data is mtcars (built-in) and your one predictor is mpg:

library(ggplot2)
library(reshape2)
mtmelt <- melt(mtcars, id = "mpg")

ggplot(mtmelt, aes(x = value, y = mpg)) +
    facet_wrap(~variable, scales = "free") +
    geom_point()

This is pretty unusual, more commonly you put predictors on the x-axis, but it's what you asked for.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
0

Actually, this a feature plot (featurePlot) in caret, for example, and is common in machine learning. A slightly more general version:

data_set <- diamonds[1:1000, c(1, 5, 6, 7, 8, 9, 10)]
response <- "price"

feature_plot <- function (data_set, response) {
  mtmelt <<- melt(data_set, id = response)
  p <- ggplot(mtmelt, aes(x = value, y = mtmelt[, 1])) +
    facet_wrap(~variable, scales = "free") +
    geom_point() +
    labs(y = response)
  p
}

feature_plot(data_set, response)
Isaiah
  • 2,091
  • 3
  • 19
  • 28