Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scatter plots for all pairwise columns between two data frames

Tags:

r

lets say I have 2 data frames:

df1 = data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10))
df2 = data.frame(d = rnorm(10), e = rnorm(10))

I would like to look at the all pairwise scatter plots between data frames:

i.e.: the six scatter plots: a vs d, a vs e, b vs d, b vs e, c vs d, c vs e.

How could I achieve this? I notice that pairs does this for a single data.frame

like image 598
kmace Avatar asked Mar 06 '26 14:03

kmace


1 Answers

use cbind to combine the two dfs and then use plot()

df1 = data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10))
df2 = data.frame(d = rnorm(10), e = rnorm(10))


df <- cbind(df1, df2)

plot(df)

enter image description here

If you want to create only plots between the two data.frames (no self comparison), you can loop them:

par(mfrow = c(ncol(df1), ncol(df2)))


for(i in 1:ncol(df1)){
  for(j in 1:ncol(df2)){
    plot(df1[,i], df2[,j], main = paste(names(df1)[i], "vs", names(df2)[j]), 
         ylab = names(df2)[j], 
         xlab = names(df1)[i])
  }
}

enter image description here

like image 94
loki Avatar answered Mar 09 '26 04:03

loki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!