Search code examples
rggplot2plotggpairs

Subsetting plots in R with pairs function


I have a simple question however I can't find the answer. I have a dataset in R with 1000 rows and 5 columns as follows:

data <- matrix(rnorm(1000 * 5, mean = 0, sd = 1), 1000, 5)
colnames(data) <-
    c("A", "B", "C", "D", "E")

I want to visually examine the relation between A and all other columns. How can I do that in R? If I wanted all the combination, I would have used the pairs(data) function. the above simulation is a very simple example but when 20 columns exist, this subset reduces the complexity and it is a less consuming process. Is there any way to make calculate and present visually the plot of A with all others and also add the trend line?


Solution

  • With plot and a for loop, you could do:

    par(mfrow = c(2, 2))
    for (i in seq(ncol(data))[-1]){
      plot(data[, 1], data[, i], xlab = "A", ylab = colnames(data)[i])
      abline(lm(data[, 1] ~ data[, i]), col = "red")
    }
    

    enter image description here