4

I have a table with column X, and several columns Y1, Y2, Y3... Y50 I'm struggling with the R syntax for doing multiple scatter plots of Y1 vs. X, Y2 vs. X etc. using a single command similar to how pair(table) works; except that I don't want every column against every other column - it's a scatter plot of each of Y vs. X

rgeekay
  • 71
  • 1
  • 4

4 Answers4

2

I think it is better to put your data in the long format, using stack or melt like this :

X <- 1:10
dat <- cbind(matrix(rnorm(10*50),ncol=50,
           dimnames=list(NULL,paste0('Y',1:50))))
dat <- cbind(X,stack(as.data.frame(dat)))

In your case you can get the matrix of Y using something like this :

 do.call(cbind,mget(ls(pattern='Y[0-9]+')))

Then plot them using ggplot2

library(ggplot2)
ggplot(dat) +
  geom_line(aes(x=X,y=values,color=ind)) +
  facet_wrap(~ind)+theme(legend.position='none')

enter image description here

or lattice:

xyplot(X~values|ind,data=dat,type='l',groups=ind)

enter image description here

agstudy
  • 119,832
  • 17
  • 199
  • 261
2

Maybe take a look at the googleVis package and the gvisScatterChart function. The input to the function is exactly in the format you have described.

https://developers.google.com/chart/interactive/docs/gallery/scatterchart

EDIT with an example.

library('googleVis')
data(iris)
plot(gvisScatterChart(iris[,1:4])) 

Note that you cannot have characters in the input data supplied so I have selected numeric columns only.

Geoffrey Absalom
  • 1,815
  • 15
  • 16
  • I would upvote if you can give an example..googleVis is great for its interactivity but in practice it is not obvious to get a result. – agstudy Jun 23 '13 at 19:00
1

Do you mean pairs? The convention that I found easiest for me to pick up is to specify the range of columns you wanted to plot against each other. for example:

dim(iris)
## five columns across
pairs(iris)
## If I only want the second, third and fourth columns. 
pairs(iris[,2:4])
cylondude
  • 1,816
  • 1
  • 22
  • 55
1

If you just want to plot for example the first row of a table or data frame against the second, third and fourth you can use

par(mfrow=c(1,3))
apply(iris[,2:4], 2, plot, x=iris[,1], xlab="x", ylab="y")

Or, alternatively a for loop might be good if you want to have control over axis labels etc:

par(mfrow=c(1,3))
for (i in 2:4){plot(iris[,1], iris[,i], xlab="iris data column #1", ylab=paste("iris data column #", i), main=paste("iris col 1 vs col ", i))}

enter image description here

Maybe the function scatterplotMatrix from the packae car could be interesting for you as well.

user1981275
  • 13,002
  • 8
  • 72
  • 101
  • +1. however, the `scatterplotMatrix`recommendation will not work out for this OP. Note in the question the OP is looking for a series of **50** plots. I would recommend dumping the plots to a pdf. – gung - Reinstate Monica Jun 23 '13 at 17:25