2

I am trying to find the relationship between a selected target variable and the other variables in a data set. For example, if I consider the auto dataset I get the following plot. Assume my target variable is mpg

library("ISLR")
pairs(Auto)

Image of the plot

However, I would like to return only the first row of this plot since I'm only interested in plotting the relationship between the target variable and each of the other variables. I have seen this post which uses the tidyverse library and rearranges my dataframe.

I'm hoping there's a way to do it using just the pairs() function in R.

Edit: I'm looking for a way to keep the variable names in the final result.

Hawklaz
  • 306
  • 4
  • 20

1 Answers1

2

Look for horInd and verInd in ?pairs.

For instance

pairs(mtcars, verInd = 1, horInd = 2:ncol(mtcars))
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
  • 1
    Thanks. That actually answers the question I asked. However, I noticed that it takes off the names of the variables in the resultant plot. Any suggestion to avoid losing them? – Hawklaz Aug 23 '20 at 06:40
  • 1
    @Hawklaz I don't think it's easy: `pairs` does not add labels to the plots except on the diagonal, so nothing is really taken off. Maybe there is a way with the `panel` argument (which is the function that makes each plot), but unless the variable name is passed as an argument, it won't be simple (maybe some trickery with a global variable?). I'll have a look. –  Aug 24 '20 at 05:36