Welcome, Guest. Please Login or Register
Algorithmic Trading Forum
 
  HomeHelpSearchLoginRegister  
 
Pages: 1 2 
Send Topic Print
question for the R experts on scatterplot colors (Read 20276 times)
statstrader
Newbie
*
Offline

I like this forum!

Posts: 27

question for the R experts on scatterplot colors
03/05/09 at 00:13:39
 
If I have a dataset that is a matrix of several
col  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 class
plot(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.
Back to top
 
 
  IP Logged
Co0olCat
Forum Moderator
*****
Offline



Posts: 128

Gender: male
Re: question for the R experts on scatterplot colors
Reply #1 - 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 plot

plot(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
Back to top
 
 

"Yesterday is history. Tomorrow is a mystery. But today is a gift, and that is why it's called the present."
WWW timuryusupov   IP Logged
statstrader
Newbie
*
Offline

I like this forum!

Posts: 27

Re: question for the R experts on scatterplot colors
Reply #2 - 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, and
do 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. Wink
----------------
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.
Back to top
 
 
  IP Logged
StatEdges
Newbie
*
Offline

Always a student!

Posts: 15

Re: question for the R experts on scatterplot colors
Reply #3 - 03/05/09 at 17:31:07
 
How about:

plot(x4~x1, pch=10, col=if(r==0, "red", "blue"))
Back to top
 
 
  IP Logged
statstrader
Newbie
*
Offline

I like this forum!

Posts: 27

Re: question for the R experts on scatterplot colors
Reply #4 - 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. Tongue
Back to top
 
 
  IP Logged
StatEdges
Newbie
*
Offline

Always a student!

Posts: 15

Re: question for the R experts on scatterplot colors
Reply #5 - 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.
Back to top
 
 
  IP Logged
statstrader
Newbie
*
Offline

I like this forum!

Posts: 27

Re: question for the R experts on scatterplot colors
Reply #6 - 03/05/09 at 21:14:40
 
That is perfect! thank you. Cheesy  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,r

and 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?
Back to top
 
 
  IP Logged
StatEdges
Newbie
*
Offline

Always a student!

Posts: 15

Re: question for the R experts on scatterplot colors
Reply #7 - 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 xmatrix1
plot(xmatrix1$x4~xmatrix1$x1, pch=ifelse(xmatrix1$r==1, "*", "+"), col=ifelse(xmatrix1$r==1, "red", "blue"))

# add xmatrix2 to the plot
points(xmatrix2$x4~xmatrix2$x1, pch=ifelse(xmatrix2$r==1, "*", "+"), col=ifelse(xmatrix2$r==1, "red", "blue"))
Back to top
 
 
  IP Logged
statstrader
Newbie
*
Offline

I like this forum!

Posts: 27

Re: question for the R experts on scatterplot colors
Reply #8 - 03/05/09 at 23:56:28
 
Literally using the pairs function to create a draftsman's plot
of the multivariate data contained in one matrix.

For example suppose you have matrix1 which contains column vectors;
x1,x2,x3,x4,r

you 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.
Back to top
 
 
  IP Logged
StatEdges
Newbie
*
Offline

Always a student!

Posts: 15

Re: question for the R experts on scatterplot colors
Reply #9 - 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.
Back to top
 
 
  IP Logged
statstrader
Newbie
*
Offline

I like this forum!

Posts: 27

Re: question for the R experts on scatterplot colors
Reply #10 - 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 Smiley

Hopefully, more bright lurkers will start joining some of the discussions here.
Back to top
 
 
  IP Logged
Algo Designer
Forum Administrator
*****
Offline

Nihil desperandum

Posts: 181
Sydney
Gender: male
Re: question for the R experts on scatterplot colors
Reply #11 - 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".
Back to top
 
 

"Success is the sum of small efforts, repeated day in and day out..."
  IP Logged
StatEdges
Newbie
*
Offline

Always a student!

Posts: 15

Re: question for the R experts on scatterplot colors
Reply #12 - 03/11/09 at 14:05:46
 
Maybe change the subject (if it's possible) to something like:

"R Help Thread"

Grin
Back to top
 
 
  IP Logged
Carfield Yim
Newbie
*
Offline

I like this forum!

Posts: 1

Re: question for the R experts on scatterplot colors
Reply #13 - 03/18/09 at 12:44:56
 
What book can help me to start understand it?
Back to top
 
 
  IP Logged
StatEdges
Newbie
*
Offline

Always a student!

Posts: 15

Re: question for the R experts on scatterplot colors
Reply #14 - 03/18/09 at 19:11:41
 
Back to top
 
 
  IP Logged
Pages: 1 2 
Send Topic Print