Skip to content

Combining several mappings in ggplot2

8 messages · Tribo Laboy, ONKELINX, Thierry, Hadley Wickham

#
Hello,

I want to be able to make a plot that has several series with
different color and linetype.
Online documentation suggest that this is possible, but I haven't found how:

"We can also create redundant mappings, mapping the same variable to
multiple aesthetics. This is most useful when producing a graphic for
both colour and black and white display."

Here's what I have to get the color
ggplot(data = Orange, aes(x = age, y = circumference, color = Tree)) +
geom_line()

if i try this I get an error:
ggplot(data = Orange, aes(x = age, y = circumference, color = Tree,
linetype = Tree)) + geom_line()

or
ggplot(data = Orange, aes(x = age, y = circumference, color = Tree)) +
geom_line() + scale_linetype_manual(value = c(1:5))

I'd be grateful to get a suggestion of how to do this.

Regards,

TL
#
On Tue, Mar 25, 2008 at 11:48 AM, Tribo Laboy <tribolaboy at gmail.com> wrote:
Please provide a reproducible example (or at least describe the
error!), otherwise we have no way to help you.

Hadley
#
Apologies! I though that the Orange dataset comes with R, but it is in
fact in the package "datasets".

So here's another "Orange2" dataset for the example:

Tree_v = rep(c(1:5),each = 5)
age_v = rep(seq(1,25, by = 5),5) + 10*runif(25)
circumference_v <- rep(seq(21,45, by = 5), 5)*Tree_v + 25*runif(25)
Orange2 <- data.frame(Tree = as.factor(Tree_v), age = age_v,
circumference = circumference_v)


This works fine:

ggplot(data = Orange2, aes(x = age, y = circumference, color = Tree))
+ geom_line()

These generate errors (included):
Error in get("check_domain", env = ., inherits = TRUE)(., ...) :
  Too many values in domain (5 > 4)
Error in unit(values, units, data = data) :
  'x' and 'units' must have length > 0


What am I doing wrong?


Regards,

TL
On Wed, Mar 26, 2008 at 2:08 AM, hadley wickham <h.wickham at gmail.com> wrote:
#
Tribo,

It looks like geom_line() accepts only 4 linetypes and you asked for 5.

library(ggplot2)
Tree_v <- rep(c(1:5),each = 5)
age_v <- rep(seq(1,25, by = 5),5) + 10*runif(25)
circumference_v <- rep(seq(21,45, by = 5), 5)*Tree_v + 25*runif(25)

#This will work
Orange2 <- data.frame(Tree = as.factor(Tree_v), age = age_v,
circumference = circumference_v, Lines = factor(as.numeric(Orange2$Tree)
%% 4))
ggplot(data = Orange2, aes(x = age, y = circumference, color = Tree,
linetype = Lines)) + geom_line()

#This won't
Orange2 <- data.frame(Tree = as.factor(Tree_v), age = age_v,
circumference = circumference_v, Lines = factor(as.numeric(Orange2$Tree)
%% 5))
ggplot(data = Orange2, aes(x = age, y = circumference, color = Tree,
linetype = Lines)) + geom_line()

HTH,

Thierry

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium 
tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be 
www.inbo.be 

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org]
Namens Tribo Laboy
Verzonden: woensdag 26 maart 2008 12:01
Aan: hadley wickham
CC: r-help op r-project.org
Onderwerp: Re: [R] Combining several mappings in ggplot2

Apologies! I though that the Orange dataset comes with R, but it is in
fact in the package "datasets".

So here's another "Orange2" dataset for the example:

Tree_v = rep(c(1:5),each = 5)
age_v = rep(seq(1,25, by = 5),5) + 10*runif(25)
circumference_v <- rep(seq(21,45, by = 5), 5)*Tree_v + 25*runif(25)
Orange2 <- data.frame(Tree = as.factor(Tree_v), age = age_v,
circumference = circumference_v)


This works fine:

ggplot(data = Orange2, aes(x = age, y = circumference, color = Tree))
+ geom_line()

These generate errors (included):
linetype = Tree)) + geom_line()
Error in get("check_domain", env = ., inherits = TRUE)(., ...) :
  Too many values in domain (5 > 4)
+ geom_line() + scale_linetype_manual(value = c(1:5))
Error in unit(values, units, data = data) :
  'x' and 'units' must have length > 0


What am I doing wrong?


Regards,

TL



On Wed, Mar 26, 2008 at 2:08 AM, hadley wickham <h.wickham op gmail.com>
wrote:
wrote:
found how:
to
for
Tree)) +
Tree,
______________________________________________
R-help op r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
Hi Thierry

Thanks for replying. I tried your code, but it spit an error on me:
+ circumference = circumference_v, Lines =
factor(as.numeric(Orange2$Tree) + %% 4))
Error: unexpected SPECIAL in:
"Orange2 <- data.frame(Tree = as.factor(Tree_v), age = age_v,
circumference = circumference_v, Lines = factor(as.numeric(Orange2$Tree) + %%"

Am I typing it correctly? What does the %% 4 or %%5 mean by the way?

Regards,

TL


On Wed, Mar 26, 2008 at 11:26 AM, ONKELINX, Thierry
<Thierry.ONKELINX at inbo.be> wrote:
#
On Wed, Mar 26, 2008 at 6:01 AM, Tribo Laboy <tribolaboy at gmail.com> wrote:
It works fine for me, which implies that it's a bug that I've fixed in
the development version.  I should be releasing a new version soon
(just as soon as I've handed in my thesis on Monday) so your best bet
is to wait for that.

Hadley
#
Hi Hadley,

I am surprised you find time to reply to r-help questions despite your
thesis deadline being on Monday. Good luck with it! Yes, I can wait
until the new version is ready!

Regards,

TL
On Thu, Mar 27, 2008 at 12:28 PM, hadley wickham <h.wickham at gmail.com> wrote:
#
Dear Tribo,

I've made a little mistake. It should have been

Orange2 <- data.frame(Tree = as.factor(Tree_v), age = age_v,
circumference = circumference_v)
Orange2$Lines <- factor(as.numeric(Orange2$Tree) %% 4)

Have a look at help("%%")

HTH,

Thierry


------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium 
tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be 
www.inbo.be 

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: Tribo Laboy [mailto:tribolaboy op gmail.com] 
Verzonden: woensdag 26 maart 2008 17:42
Aan: ONKELINX, Thierry
CC: hadley wickham; r-help op r-project.org
Onderwerp: Re: [R] Combining several mappings in ggplot2

Hi Thierry

Thanks for replying. I tried your code, but it spit an error on me:
+ circumference = circumference_v, Lines =
factor(as.numeric(Orange2$Tree) + %% 4))
Error: unexpected SPECIAL in:
"Orange2 <- data.frame(Tree = as.factor(Tree_v), age = age_v,
circumference = circumference_v, Lines = factor(as.numeric(Orange2$Tree)
+ %%"

Am I typing it correctly? What does the %% 4 or %%5 mean by the way?

Regards,

TL


On Wed, Mar 26, 2008 at 11:26 AM, ONKELINX, Thierry
<Thierry.ONKELINX op inbo.be> wrote:
5.
factor(as.numeric(Orange2$Tree)
factor(as.numeric(Orange2$Tree)
------------------------------------------------------------------------
Nature
more
to
not
[mailto:r-help-bounces op r-project.org]
in
Tree,
Tree))
<tribolaboy op gmail.com>
haven't
variable
graphic