[R] ggplot seq

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Sat Jan 24 17:40:13 CET 2009


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 at 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 at yahoo.com] 
Verzonden: vrijdag 23 januari 2009 18:44
Aan: r-help at 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 at inbo.be> wrote:

> From: ONKELINX, Thierry <Thierry.ONKELINX at 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=21,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.




More information about the R-help mailing list