Home > database >  Subsetting plots in R with pairs function
Subsetting plots in R with pairs function

Time:08-05

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?

CodePudding user response:

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

CodePudding user response:

Since the question has a ggplot2 tag, here's how you could achieve this in the tidyverse without any loops, and with trend lines added.

library(tidyverse)

data %>%
  as.data.frame() %>%
  pivot_longer(-A) %>%
  ggplot(aes(A, value))  
  geom_point()  
  geom_smooth(method = "lm", formula = y~x)  
  facet_wrap(.~name)  
  theme_bw(base_size = 16)

Created on 2022-08-04 by the reprex package (v2.0.1)

  • Related