Skip to content

Split plot in colors/pch??

5 messages · Joerg Maeder, Fredrik Karlsson, Peter Dalgaard +1 more

#
Hi all

This seems like a very easy question, but I have not found a simple
answer to it:

If you have a dataframe, how to you plot two of then in different colors
depending on a third variable?


All help appreciated!

/Fredrik
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Hi Fredrik,

here is a very small example that should show the way to do it

d <- data.frame(x=c(1,2,3,4,5,6,4,3),y=1:8,g=c(1,2,1,2,3,1,2,3))#g
defines the group (color)
co <- c('red','green','blue')
plot(d$x,d$y,col=co[d$g])

same way for pch

gruess

joerg
Fredrik Karlsson wrote:

  
    
#
Thank you Joerg, but that was not quite what I wanted.
What I was asking was how to plot d$x ~ d$y split into separate colours
depenging on a third variable (such as for instance
d$gender=c(F,F,M,M,F,F,M,M)).

So, I want to plot x~y where gender==F in one colour and gender==M in
another.

Hope that califies my question.

/Fredrik
On Fri, Mar 01, 2002 at 01:42:53PM +0100, Joerg Maeder wrote:

  
    
#
Fredrik Karlsson <fredrik.karlsson at ling.umu.se> writes:
Not really. As far as I can tell, that is exactly what Joerg's code
does...

  
    
#
If I understand it correctly you want to plot variable y against 
variable x but with plotting characters or colours coded by z 
values.

I usually use this construction

plot(daf$x,daf$y,type="n")

followed by 

points(daf$x, daf$y, pch=daf$z, col=daf$z)

if z has reasonable values }integer 1:7

or construction with for loop

for (i in 1:n) points(daf$x[selection according i],, daf$y[sel......], 
pch=i, col=i)

and sometimes matplot is quite convenient (see help pages).

Meanwhile for good graphic tricks I usually consult StatsRus

http://lark.cc.ukans.edu/~pauljohn/R/statsRus.html
On 1 Mar 2002 at 11:28, Fredrik Karlsson wrote:

            
Petr Pikal
petr.pikal at precheza.cz
p.pik at volny.cz


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._