FAQ
Hi all

This seems like a very easy question, but I have not found a simple

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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

## Search Discussions

•  at Mar 1, 2002 at 12:42 pm ⇧
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:
Hi all

This seems like a very easy question, but I have not found a simple

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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
--
Joerg Maeder .:|:||:..:.||.:: maeder at atmos.umnw.ethz.ch
Tel: +41 1 633 36 25 .:|:||:..:.||.::
http://www.iac.ethz.ch/staff/maeder
PhD student at INSTITUTE FOR ATMOSPHERIC AND CLIMATE SCIENCE (IACETH)
ETH Z?RICH Switzerland
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
•  at Mar 1, 2002 at 12:57 pm ⇧
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:
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:
Hi all

This seems like a very easy question, but I have not found a simple

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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
--
Joerg Maeder .:|:||:..:.||.:: maeder at atmos.umnw.ethz.ch
Tel: +41 1 633 36 25 .:|:||:..:.||.::
http://www.iac.ethz.ch/staff/maeder
PhD student at INSTITUTE FOR ATMOSPHERIC AND CLIMATE SCIENCE (IACETH)
ETH Z?RICH Switzerland
--
-----------------------------------------------------------------
Fredrik Karlsson, Research Student

Department of Philosophy and Linguistics
Ume? University
S-901 87 UME?
SWEDEN
Tel: +46 90 786 56 84 Web: http://www.ling.umu.se/~fredrik/
Fax: +46 90 786 63 77 Email: fredrik at ling.umu.se
PGP-fingerprint:2DD5 54A1 4555 6D00 F386 C269 C430 0C0F 1A53 49B9
-----------------------------------------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
•  at Mar 1, 2002 at 1:17 pm ⇧

Fredrik Karlsson <fredrik.karlsson@ling.umu.se> writes:

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.
Not really. As far as I can tell, that is exactly what Joerg's code
does...

/Fredrik
On Fri, Mar 01, 2002 at 01:42:53PM +0100, Joerg Maeder wrote:
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:
Hi all

This seems like a very easy question, but I have not found a simple

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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
--
Joerg Maeder .:|:||:..:.||.:: maeder at atmos.umnw.ethz.ch
Tel: +41 1 633 36 25 .:|:||:..:.||.::
http://www.iac.ethz.ch/staff/maeder
PhD student at INSTITUTE FOR ATMOSPHERIC AND CLIMATE SCIENCE (IACETH)
ETH Z?RICH Switzerland
--
-----------------------------------------------------------------
Fredrik Karlsson, Research Student

Department of Philosophy and Linguistics
Ume? University
S-901 87 UME?
SWEDEN
Tel: +46 90 786 56 84 Web: http://www.ling.umu.se/~fredrik/
Fax: +46 90 786 63 77 Email: fredrik at ling.umu.se
PGP-fingerprint:2DD5 54A1 4555 6D00 F386 C269 C430 0C0F 1A53 49B9
-----------------------------------------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
•  at Mar 1, 2002 at 1:27 pm ⇧
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:

Hi all

This seems like a very easy question, but I have not found a simple

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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._._._._
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
•  at Mar 1, 2002 at 2:43 pm ⇧
Actually Jeorg's answer works just fine for this case, with one small change

# create the data frame (g is gender)
d <- data.frame(x=c(1,2,3,4,5,6,4,3),y=1:8,
g=c('M','F','M','F','F','M','F','F'))

# plot using specified colors
co <- c('red','blue')
plot(d\$x,d\$y,col=co[d\$g])

# plot using default colors
plot(d\$x,d\$y,col=codes(d\$g))

-Greg

-----Original Message-----
From: Fredrik Karlsson [mailto:fredrik.karlsson at ling.umu.se]
Sent: Friday, March 01, 2002 7:58 AM
To: 'R-help at lists.R-project.org'
Subject: Re: [R] Split plot in colors/pch??

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:
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:
Hi all

This seems like a very easy question, but I have not
found a simple

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

All help appreciated!

/Fredrik
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._._._._._._._._
--
Joerg Maeder .:|:||:..:.||.:: maeder at atmos.umnw.ethz.ch
Tel: +41 1 633 36 25 .:|:||:..:.||.::
http://www.iac.ethz.ch/staff/maeder
PhD student at INSTITUTE FOR ATMOSPHERIC AND CLIMATE
SCIENCE (IACETH)
ETH Z?RICH Switzerland
--
-----------------------------------------------------------------
Fredrik Karlsson, Research Student

Department of Philosophy and Linguistics
Ume? University
S-901 87 UME?
SWEDEN
Tel: +46 90 786 56 84 Web: http://www.ling.umu.se/~fredrik/
Fax: +46 90 786 63 77 Email: fredrik at ling.umu.se
PGP-fingerprint:2DD5 54A1 4555 6D00 F386 C269 C430 0C0F 1A53 49B9
-----------------------------------------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._._._._._._._._

LEGAL NOTICE
Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
•  at Mar 4, 2002 at 1:58 am ⇧
Hi

You might also want to look at using xyplot() and panel.superpose from the
lattice package. For example, ...

library(lattice)
data(iris)
attach(iris)
xyplot(Sepal.Length ~ Sepal.Width, groups=Species,
panel=panel.superpose)

Paul

Actually Jeorg's answer works just fine for this case, with one small change
# create the data frame (g is gender)
d <- data.frame(x=c(1,2,3,4,5,6,4,3),y=1:8,
g=c('M','F','M','F','F','M','F','F'))

# plot using specified colors
co <- c('red','blue')
plot(d\$x,d\$y,col=co[d\$g])

# plot using default colors
plot(d\$x,d\$y,col=codes(d\$g))

-Greg

-----Original Message-----
From: Fredrik Karlsson [mailto:fredrik.karlsson at ling.umu.se]
Sent: Friday, March 01, 2002 7:58 AM
To: 'R-help at lists.R-project.org'
Subject: Re: [R] Split plot in colors/pch??

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:
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:
Hi all

This seems like a very easy question, but I have not
found a simple

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

All help appreciated!

/Fredrik
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._._._._._._._._
--
Joerg Maeder .:|:||:..:.||.:: maeder at atmos.umnw.ethz.ch
Tel: +41 1 633 36 25 .:|:||:..:.||.::
http://www.iac.ethz.ch/staff/maeder
PhD student at INSTITUTE FOR ATMOSPHERIC AND CLIMATE
SCIENCE (IACETH)
ETH Z?RICH Switzerland
--
-----------------------------------------------------------------
Fredrik Karlsson, Research Student

Department of Philosophy and Linguistics
Ume? University
S-901 87 UME?
SWEDEN
Tel: +46 90 786 56 84 Web: http://www.ling.umu.se/~fredrik/
Fax: +46 90 786 63 77 Email: fredrik at ling.umu.se
PGP-fingerprint:2DD5 54A1 4555 6D00 F386 C269 C430 0C0F 1A53 49B9
-----------------------------------------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._._._._._._._._

LEGAL NOTICE
Unless expressly stated otherwise, this message is confidential and may be
by anyone else is unauthorized. If you are not an addressee, any disclosure
or copying of the contents of this E-mail or any action taken (or not taken)
in reliance on it is unauthorized and may be unlawful. If you are not an
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._
>

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

## Related Discussions

Discussion Overview
 group r-help categories r posted Mar 1, '02 at 10:28a active Mar 4, '02 at 1:58a posts 7 users 6 website r-project.org irc #r

### 6 users in discussion

Content

People

Support

Translate

site design / logo © 2018 Grokbase