Algorithmic Trading Group Forum https://www.algotradinggroup.com/cgi-bin/yabb2/YaBB.pl Trading Strategies >> Software Infrastructure >> question for the R experts on scatterplot colors https://www.algotradinggroup.com/cgi-bin/yabb2/YaBB.pl?num=1236212020 Message started by statstrader on 03/05/09 at 00:13:39

 Title: question for the R experts on scatterplot colors Post by statstrader on 03/05/09 at 00:13:39 If I have a dataset that is a matrix of severalcol  variables, and the one output col variable is a class of 1 or 0, can someone give an R example, whereby the classes are differentiated by color?ex:  x1,x2,x3,x4,r where r is classplot(x4 ~ x1, pch=16) works for regular scatterplot.now, how to modify same scatterplot, with x4~x1 points being some color (say = red for r =1 and say blue for r=0).Thanks.

 Title: Re: question for the R experts on scatterplot colors Post by Co0olCat on 03/05/09 at 00:27:26 The easy way is to use the following:plot(x4 ~ x1, pch=16, col = "blue")par(new = T) # Keep existing plotplot(x4 ~ x1, pch=16, col = "red")Note: you need to be careful with xlim and ylim. I would first find min and max for each plot and then pass it as an argument.Hope it helps.P.S. I am not an expert. Usually one can find all the answers here http://finzi.psych.upenn.edu/search.html

 Title: Re: question for the R experts on scatterplot colors Post by statstrader on 03/05/09 at 00:37:00 That is closer, thank you. I needed the par command.  Although, it still does not differentiate classes.I can, however, now do so by:plot(x4~x1, pch=10, col=r)par(new=T)plot(x4~x1,pch=12, col=!r)this works great to separate  the two classes(i.e. one pch=10 for r=1, pch=12 for r=0).The only problem is I lose color attributes, anddo not know enough to add the additional color attribute to col=r, cannot just add col=r, col='red' and also tried to create a vector att<- c(r,'red') which only worked for 1 dot.Much closer though. ;)----------------edit: manual way to do it.If you know the count of classes, then let c0 = count(r such that c=0)let c1 = count(r such that c=1)then create vector:cv <- c(rep("red",c0), rep("blue",c1),r)then plot as follows,plot(x4 ~ x1, pch=12, col=cv)works like a charm... only you have to count instances of each class.

 Title: Re: question for the R experts on scatterplot colors Post by Random on 03/05/09 at 17:31:07 How about:plot(x4~x1, pch=10, col=if(r==0, "red", "blue"))

 Title: Re: question for the R experts on scatterplot colors Post by statstrader on 03/05/09 at 17:43:05 Hi,Although that intuitively makes sense a bit, a working solution is a bit more specific to the exact example.  The solution I showed last is one way that works.Now I'm trying to figure how to extend it to pairs.  You would think anyone working in classification problems would have encountered this before. :P

 Title: Re: question for the R experts on scatterplot colors Post by Random on 03/05/09 at 18:30:54 Oops - had a typo - should be:plot(x4~x1, pch=10, col=ifelse(r==1, "red", "blue")) This should work as per your example.  If you also want to change the plot character:plot(x4~x1, pch=ifelse(r==1, "*", "+"), col=ifelse(r==1, "red", "blue")) Also the command "points" allows you to add to the existing plot instead of using the "par" commmand.

 Title: Re: question for the R experts on scatterplot colors Post by statstrader on 03/05/09 at 21:14:40 That is perfect! thank you. :D  Much more efficient than my example.Now, do you have any idea how to extend that concept to pairs?I.e. suppose you have Xmatrix = x1,x2,x3,x4,rand you want to plot pairs of Xmatrix only showing relationships of x1 to x4, BUT with color corresponding to r being class 0 or 1.Any ideas?

 Title: Re: question for the R experts on scatterplot colors Post by Random on 03/05/09 at 22:19:18 Could you elaborate a bit more?If you mean you have 2 Xmatrix(es) and you want to put both on the same plot, then:# plot xmatrix1plot(xmatrix1\$x4~xmatrix1\$x1, pch=ifelse(xmatrix1\$r==1, "*", "+"), col=ifelse(xmatrix1\$r==1, "red", "blue")) # add xmatrix2 to the plotpoints(xmatrix2\$x4~xmatrix2\$x1, pch=ifelse(xmatrix2\$r==1, "*", "+"), col=ifelse(xmatrix2\$r==1, "red", "blue"))

 Title: Re: question for the R experts on scatterplot colors Post by statstrader on 03/05/09 at 23:56:28 Literally using the pairs function to create a draftsman's plotof the multivariate data contained in one matrix.For example suppose you have matrix1 which contains column vectors;x1,x2,x3,x4,ryou could just do pairs(matrix1) and it will generate a draftsman plot of the scatterplots for each pair.  However, the data are all one color, and I don't want r, which is a class variable, to be in the draftsman plot.Instead, I would like to do a pair plot on just the x1 through x4, with color of each coordinate being a function of whether the r column is class 0 or class 1.  Exactly the same as the prior problem with one plot.However, this takes into account multiple plots simultaneously.

 Title: Re: question for the R experts on scatterplot colors Post by Random on 03/06/09 at 18:02:06 Try:pairs(Xmatrix[,1:4], col=ifelse(r==1, "red", "blue"))Though I would verify the results to make sure it's what you're hoping.Good luck!P.S. Perhaps we should have a general R Q&A sticky for these sorts of questions.

 Title: Re: question for the R experts on scatterplot colors Post by statstrader on 03/06/09 at 22:54:14 That worked perfectly! Yes, an R questionnaire thread or sticky would be great.  I'm sure you will also save a lot of R folks  who stumble upon this through google; I tried googling 1st but never found so precise an answer.Thanks Again, Random :) Hopefully, more bright lurkers will start joining some of the discussions here.

Title: Re: question for the R experts on scatterplot colors
Post by Algo Designer on 03/09/09 at 14:25:14

statstrader wrote on 03/06/09 at 22:54:14:
 That worked perfectly! Yes, an R questionnaire thread or sticky would be great.  I'm sure you will also save a lot of R folks  who stumble upon this through google; I tried googling 1st but never found so precise an answer....

That's a good suggestion. I have made this topic "sticky".

 Title: Re: question for the R experts on scatterplot colors Post by Random on 03/11/09 at 14:05:46 Maybe change the subject (if it's possible) to something like:"R Help Thread" ;D

 Title: Re: question for the R experts on scatterplot colors Post by Carfield Yim on 03/18/09 at 12:44:56 What book can help me to start understand it?

 Title: Re: question for the R experts on scatterplot colors Post by Random on 03/18/09 at 19:11:41 This should get you started:http://cran.r-project.org/doc/manuals/R-intro.pdf

 Title: Re: question for the R experts on scatterplot colors Post by Yury R on 03/18/09 at 19:17:25 Sorry to barge in. But having spent some time learning R I would point at one strange thing. Books on R (there are plenty) are generally well-written and are mostly in "example based" style. But they all don't start from start. They assume that you have some familiarity with it (or S) or don't need to know the language, just commands. R documentation refers to S manual (which I cannot find on Internet) very often as well. And that makes things slightly longer than really necessary.If you take, for example, "The R Book" the introduction to language is terrible - it refers to items described later in text (just tell me who invented this style as I encounter it so often). Out of all, probably the smoothest book is "Data Analysis and Graphics Using R: An Example-based Approach" by Maindonald.Perhaps, somebody would recommend something else better.

 Title: Re: question for the R experts on scatterplot colors Post by statstrader on 03/18/09 at 20:31:50 Hi Yury,One of the things that helped me learn R quickly (although I am obviously, by no means an expert), is to find a topic that interests you, you are already familiar with, or you would like to learn more about.  For example: multivariate statistical methods--http://www.amazon.com/Companion-Multivariate-Analysis-Springer-Statistics/dp/1852338822/ref=sr_1_18?ie=UTF8&s=books&qid=1237404519&sr=8-18Then find an related R book from some publisher like springer.  Most of the texts they produce have the complete R code at the author's website and include commentary in the code.There is a fantastic amount of useful information available on the web if you search deep enough.The only problem I have, is when a small detail like my original question pops up.  The R help forums can be a bit dry and convoluted, which is why it's great to have a forum like this.