FAQ
Hi Hadley: Not sure if you received my email, so I am resending it again.
I have dealt with this before and I can't remember how it got resolved. It is too much data to reproduce the example below(49.000 records) but all I am after is trying to get the x axis breaks. I want my x axis to go from 27 to 51 and 1 to 25 by 2. I am trying to concatenate the breaks but it sorts the seq() in ascending order. Is there a way to workaround it? see scale_x_continuous below:

boxP <- ggplot(WFBox, aes(Week, FL,group=Week))
boxP + geom_boxplot(outlier.colour="pink",outlier.size=3,outlier.shape!,fill="goldenrod",colour="blue") + scale_x_discrete(breaks=c(seq(27,51,2),seq(1,25,2)))

Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA

## Search Discussions

•  at Jan 23, 2009 at 11:01 am ⇧
Dear Felipe,

Provide a dummy sample if your dataset is big or confidential. The
actual values are not that important to figure out what kind of plot you
want.
How did you code Week? Numeric? Try convert it into a factor with levels
= c(27:52, 1:26). And then set the breaks to seq(1, 52, by = 2).

WFBox <- data.frame(Week = rep(1:52, 10), FL = rnorm(520))
WFBox\$fWeek <- factor(WFBox\$Week, levels = c(27:52, 1:26))
library(ggplot2)
ggplot(WFBox, aes(fWeek, FL)) +
geom_boxplot(outlier.colour="pink",outlier.size=3,outlier.shape!,fill"goldenrod",colour="blue") + scale_x_discrete(breaks=c(seq(1,51,2)))

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 Felipe Carrillo
Verzonden: donderdag 22 januari 2009 23:54
Aan: r-help op stat.math.ethz.ch
Onderwerp: [R] ggplot seq

Hi Hadley: Not sure if you received my email, so I am resending it
again.
I have dealt with this before and I can't remember how it got resolved.
It is too much data to reproduce the example below(49.000 records) but
all I am after is trying to get the x axis breaks. I want my x axis to
go from 27 to 51 and 1 to 25 by 2. I am trying to concatenate the breaks
but it sorts the seq() in ascending order. Is there a way to workaround
it? see scale_x_continuous below:

boxP <- ggplot(WFBox, aes(Week, FL,group=Week))
boxP +
geom_boxplot(outlier.colour="pink",outlier.size=3,outlier.shape!,fill"goldenrod",colour="blue") +
scale_x_discrete(breaks=c(seq(27,51,2),seq(1,25,2)))

Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA

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

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in this message
and any annex are purely those of the writer and may not be regarded as stating
an official position of INBO, as long as the message is not confirmed by a duly
signed document.
•  at Jan 23, 2009 at 3:19 pm ⇧
Thanks Thierry:
The use of levels is what I needed, thanks for your help.

From: ONKELINX, Thierry <thierry.onkelinx@inbo.be>
Subject: RE: [R] ggplot seq
To: mazatlanmexico at yahoo.com, r-help at stat.math.ethz.ch
Date: Friday, January 23, 2009, 3:01 AM
Dear Felipe,

Provide a dummy sample if your dataset is big or
confidential. The actual values are not that important to figure out what
kind of plot you want.
How did you code Week? Numeric? Try convert it into a
factor with levels = c(27:52, 1:26). And then set the breaks to seq(1, 52, by= 2).

WFBox <- data.frame(Week = rep(1:52, 10), FL = rnorm(520))
WFBox\$fWeek <- factor(WFBox\$Week, levels = c(27:52, 1:26))
library(ggplot2)
ggplot(WFBox, aes(fWeek, FL)) + geom_boxplot(outlier.colour="pink",outlier.size=3,outlier.shape!,fill "goldenrod",colour="blue") + scale_x_discrete(breaks=c(seq(1,51,2)))

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 at inbo.be www.inbo.be
Onderwerp: [R] ggplot seq

I have dealt with this before and I can't remember how
it got resolved.
It is too much data to reproduce the example below(49.000
records) but
all I am after is trying to get the x axis breaks. I want
my x axis to
go from 27 to 51 and 1 to 25 by 2. I am trying to
concatenate the breaks
but it sorts the seq() in ascending order. Is there a way
to workaround
it? see scale_x_continuous below:

boxP <- ggplot(WFBox, aes(Week, FL,group=Week))
boxP +
geom_boxplot(outlier.colour="pink",outlier.size=3,outlier.shape!,fill> "goldenrod",colour="blue") +
scale_x_discrete(breaks=c(seq(27,51,2),seq(1,25,2)))

Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA
•  at Jan 23, 2009 at 5:44 pm ⇧
Actually 'levels' works OK by ordering the x axis labels but since I have 52 weeks it gets too crowded.
Here's part of my dataset with a reproducible example.

sampDat <- "Week FryPassage
27 665
28 2232
29 9241
30 28464
31 41049
32 82216
33 230411
34 358541
35 747839
36 459682
37 609567
38 979475
39 837189
40 429016
41 523436
42 304785
43 125005
44 28047
45 5141
46 7503
47 2273
48 1065
49 0
50 0
51 0
52 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 0
18 0
19 0
20 0
21 0
22 0
23 0
24 0
25 0
26 0"
WFBar <- read.table(textConnection(sampDat), header = TRUE)
WFBar
# Bar graph (option # 1)
options(scipen=3)
bargraph <- qplot(factor(Week,levels=c(27:52,1:26)),FryPassage,
data=WFBar,geom="bar",fill=I("grey65"),colour=I("goldenrod"),
ylab="Numb of Fish",xlab="Week")
bargraph # levels give me the desired X axis order but it is too crowded

# Bar graph (option # 2) If I skip levels then I don't get to see the bars for the skipped levels
options(scipen=3)
WFBar\$Week <- factor(WFBar\$Week,levels=c('27','29','31','33','35','37','39','41','43','45','47','49','51','1','5','10','15','20','25'))
bargraph <- qplot(WFBar\$Week,FryPassage,data=WFBar,geom="bar",fill=I("grey65"),colour=I("goldenrod"),
ylab="Numb of Fish",xlab="Week")
bargraph
# It seems that thickmarks is what I need here. I was trying to concatenate c(seq(27,51,2),seq(1,25,2)) like someone else
#suggested but this doesn't seem to work with scale_x_continuous(breaks= c(seq(27,51,2),seq(1,25,2))).
# So, my question here is: How can I use the seq() function to create my custom thick marks along the x axis (same order
# as the WFBar object skipping one week in between?

--- On Fri, 1/23/09, ONKELINX, Thierry wrote:
From: ONKELINX, Thierry <thierry.onkelinx@inbo.be>
Subject: RE: [R] ggplot seq
To: mazatlanmexico at yahoo.com, r-help at stat.math.ethz.ch
Date: Friday, January 23, 2009, 3:01 AM
Dear Felipe,

Provide a dummy sample if your dataset is big or
confidential. The
actual values are not that important to figure out what
kind of plot you
want.
How did you code Week? Numeric? Try convert it into a
factor with levels
= c(27:52, 1:26). And then set the breaks to seq(1, 52, by
= 2).

WFBox <- data.frame(Week = rep(1:52, 10), FL > rnorm(520))
WFBox\$fWeek <- factor(WFBox\$Week, levels = c(27:52,
1:26))
library(ggplot2)
ggplot(WFBox, aes(fWeek, FL)) +
geom_boxplot(outlier.colour="pink",outlier.size=3,outlier.shape!,fill> "goldenrod",colour="blue") +
scale_x_discrete(breaks=c(seq(1,51,2)))

HTH,

Thierry
•  at Jan 24, 2009 at 4:40 pm ⇧
Dear Felipe,

You will need to do some reading on factors. Although the labels of a
factor can be 'numeric' try are actually just string representing
numbers. Internally factors are coded as numbers: 1 for the first level,
2 for the second and so on. That's why you run into trouble with
as.numeric() of a factor.
A <- factor(c(2, 3, 1, 4), levels = c(3, 4, 2, 1))
A
[1] 2 3 1 4
Levels: 3 4 2 1
as.numeric(A)
[1] 3 1 4 2
levels(A)
[1] "3" "4" "2" "1"
as.numeric(levels(A))[A]
[1] 2 3 1 4

Defining the factor levels and defining what labels to display on the
axis are two separate things. Removing the unwanting labels from the
levels will create missing values.
A <- factor(c(2, 3, 1, 4), levels = c(3, 1))
A
[1] <NA> 3 1 <NA>
Levels: 3 1

With factor you should use scale_x_discrete and not scale_x_continuous.
Note that I already gave you an example on how to reduce the number of
labels with scale_x_discrete. Read the argument breaks = seq(from = 1,
to = 52, by = 4) as put a sequence of tickmark (breaks) starting from
the first level to the 52th level and skip 4 levels between two ticks.

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: Felipe Carrillo [mailto:mazatlanmexico op yahoo.com]
Verzonden: vrijdag 23 januari 2009 18:44
Aan: r-help op stat.math.ethz.ch; ONKELINX, Thierry
Onderwerp: RE: [R] ggplot seq

Actually 'levels' works OK by ordering the x axis labels but since I
have 52 weeks it gets too crowded.
Here's part of my dataset with a reproducible example.

sampDat <- "Week FryPassage
27 665
28 2232
29 9241
30 28464
31 41049
32 82216
33 230411
34 358541
35 747839
36 459682
37 609567
38 979475
39 837189
40 429016
41 523436
42 304785
43 125005
44 28047
45 5141
46 7503
47 2273
48 1065
49 0
50 0
51 0
52 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 0
18 0
19 0
20 0
21 0
22 0
23 0
24 0
25 0
26 0"
WFBar <- read.table(textConnection(sampDat), header = TRUE)
WFBar
# Bar graph (option # 1)
options(scipen=3)
bargraph <- qplot(factor(Week,levels=c(27:52,1:26)),FryPassage,
data=WFBar,geom="bar",fill=I("grey65"),colour=I("goldenrod"),
ylab="Numb of Fish",xlab="Week")
bargraph # levels give me the desired X axis order but it is too
crowded

# Bar graph (option # 2) If I skip levels then I don't get to see
the bars for the skipped levels
options(scipen=3)
WFBar\$Week <-
factor(WFBar\$Week,levels=c('27','29','31','33','35','37','39','41','43',
'45','47','49','51','1','5','10','15','20','25'))
bargraph <-
qplot(WFBar\$Week,FryPassage,data=WFBar,geom="bar",fill=I("grey65"),colou
r=I("goldenrod"),
ylab="Numb of Fish",xlab="Week")
bargraph
# It seems that thickmarks is what I need here. I was trying to
concatenate c(seq(27,51,2),seq(1,25,2)) like someone else
#suggested but this doesn't seem to work with
scale_x_continuous(breaks= c(seq(27,51,2),seq(1,25,2))).
# So, my question here is: How can I use the seq() function to create
my custom thick marks along the x axis (same order
# as the WFBar object skipping one week in between?

--- On Fri, 1/23/09, ONKELINX, Thierry <Thierry.ONKELINX op inbo.be> wrote:
From: ONKELINX, Thierry <Thierry.ONKELINX op inbo.be>
Subject: RE: [R] ggplot seq
To: mazatlanmexico op yahoo.com, r-help op stat.math.ethz.ch
Date: Friday, January 23, 2009, 3:01 AM
Dear Felipe,

Provide a dummy sample if your dataset is big or
confidential. The
actual values are not that important to figure out what
kind of plot you
want.
How did you code Week? Numeric? Try convert it into a
factor with levels
= c(27:52, 1:26). And then set the breaks to seq(1, 52, by
= 2).

WFBox <- data.frame(Week = rep(1:52, 10), FL > rnorm(520))
WFBox\$fWeek <- factor(WFBox\$Week, levels = c(27:52,
1:26))
library(ggplot2)
ggplot(WFBox, aes(fWeek, FL)) +
geom_boxplot(outlier.colour="pink",outlier.size=3,outlier.shape!,fill> "goldenrod",colour="blue") +
scale_x_discrete(breaks=c(seq(1,51,2)))

HTH,

Thierry

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in this message
and any annex are purely those of the writer and may not be regarded as stating
an official position of INBO, as long as the message is not confirmed by a duly
signed document.
•  at Jan 24, 2009 at 7:03 pm ⇧
Dear Thierry:
Thanks for the factor/level advice. I was actually reading about it last night and playing further with my data I was able to make it work with the code below:

options(scipen=3)
bargraph <- qplot(factor(Week,levels=c(27:52,1:26)),FryPassage,
data=WFBar,geom="bar",fill=I("grey65"),colour=I("goldenrod"),
ylab="Numb of Fish",xlab="Week") + scale_x_discrete(breaks=seq(1,52,2))
bargraph

That's exactly the same thing you are saying about levels and factors. This is a great learning experience. Thanks again.

Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA

--- On Sat, 1/24/09, ONKELINX, Thierry wrote:
From: ONKELINX, Thierry <thierry.onkelinx@inbo.be>
Subject: RE: [R] ggplot seq
To: mazatlanmexico at yahoo.com, r-help at stat.math.ethz.ch
Date: Saturday, January 24, 2009, 8:40 AM
Dear Felipe,

You will need to do some reading on factors. Although the
labels of a
factor can be 'numeric' try are actually just
string representing
numbers. Internally factors are coded as numbers: 1 for the
first level,
2 for the second and so on. That's why you run into
trouble with
as.numeric() of a factor.
A <- factor(c(2, 3, 1, 4), levels = c(3, 4, 2, 1))
A
[1] 2 3 1 4
Levels: 3 4 2 1
as.numeric(A)
[1] 3 1 4 2
levels(A)
[1] "3" "4" "2" "1"
as.numeric(levels(A))[A]
[1] 2 3 1 4

Defining the factor levels and defining what labels to
display on the
axis are two separate things. Removing the unwanting labels
from the
levels will create missing values.
A <- factor(c(2, 3, 1, 4), levels = c(3, 1))
A
[1] <NA> 3 1 <NA>
Levels: 3 1

With factor you should use scale_x_discrete and not
scale_x_continuous.
Note that I already gave you an example on how to reduce
the number of
labels with scale_x_discrete. Read the argument breaks =
seq(from = 1,
to = 52, by = 4) as put a sequence of tickmark (breaks)
starting from
the first level to the 52th level and skip 4 levels between
two ticks.

HTH,

Thierry

## Related Discussions

Discussion Navigation
 view thread | post
Discussion Overview
 group r-help categories r posted Jan 22, '09 at 10:53p active Jan 24, '09 at 7:03p posts 6 users 2 website r-project.org irc #r

### 2 users in discussion

Content

People

Support

Translate

site design / logo © 2018 Grokbase