[R] ggplot seq

Felipe Carrillo mazatlanmexico at yahoo.com
Sat Jan 24 20:03:14 CET 2009


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




More information about the R-help mailing list