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
  • 46,390
  • 10
  • 104
  • 180
runningbirds
  • 5,411
  • 11
  • 48
  • 84

2 Answers2

2

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
  • 119,032
  • 17
  • 152
  • 277
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
  • 667
  • 1
  • 8
  • 15