R - multiple scattering diagrams y compared to x

I have a table with column X, and several columns Y1, Y2, Y3 ... Y50 I am struggling with the R syntax to execute multiple scatter diagrams Y1 compared to X, Y2 compared to X, etc. Using a single command similar to how the pair works (table); except that I don't want each column to be matched with every other column - this is a scatter plot of each of Y versus X

+4
source share
4 answers

I think it is better to place your data in a long format using stack or melt as follows:

 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 Y using something like this:

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

Then build 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

+2
source

Perhaps look at the googleVis package and the gvisScatterChart function. The entry to the function is exactly in the format that you described.

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

EDIT with an example.

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

Please note that you cannot have characters in the input, so I selected only numeric columns.

+2
source

Do you mean pairs ? The agreement that I found the easiest for me was to indicate the range of columns that you wanted to build against each other. eg:

 dim(iris) ## five columns across pairs(iris) ## If I only want the second, third and fourth columns. pairs(iris[,2:4]) 
+1
source

If you just want to build, for example, the first row of a table or data in 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 nice 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

You might be interested in the scatterplotMatrix function from car packages.

+1
source

Source: https://habr.com/ru/post/1487743/


All Articles