[R] Order labels in qplot() - ggplot2 {help}

William Dunlap wdunlap at tibco.com
Thu Jun 10 21:47:45 CEST 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of baptiste auguie
> Sent: Thursday, June 10, 2010 11:50 AM
> To: Kim Jung Hwa
> Cc: r-help at r-project.org; ggplot2
> Subject: Re: [R] Order labels in qplot() - ggplot2 {help}
> 
> Hi,
> 
> You could reorder the factor levels before plotting,
> 
> x$n = factor(x$n, levels=c("va","vp", letters[1:3]))
> 
> last_plot() %+% x
> 
> or you could avoid using factors in the first place,
> 
>  x <- data.frame(cbind(n,p,pm,pn), stringsAsFactors=FALSE)

The idiom
    data.frame(cbind(x,y,z))
is almost always bad and should be replaced by
    data.frame(x,y,z)

If x, y, and z are ordinary vectors then the cbind()
in the former converts them all to be a common type
so it can make a matrix out of them.  If some of the
x, y, and z are factors and some are numeric the common
type will be numeric.  If some are character and some
are numeric the common type will be character.

data.frame(cbind(x,y,z)) will then take the matrix made
by cbind(x,y,z), split it into its columns, and if the
columns are character it will convert them to factors.

You will probably be surprised by the results of this:
  > str(data.frame(cbind(Char=letters[1:3], Num=11:13)))
  'data.frame':   3 obs. of  2 variables:
   $ Char: Factor w/ 3 levels "a","b","c": 1 2 3
   $ Num : Factor w/ 3 levels "11","12","13": 1 2 3
  > str(data.frame(cbind(Char=letters[1:3], Num=11:13),
      stringsAsFactors=FALSE))
  'data.frame':   3 obs. of  2 variables:
   $ Char: chr  "a" "b" "c"
   $ Num : chr  "11" "12" "13"
  > str(data.frame(cbind(Char=factor(letters[1:3]), Num=11:13)))
  'data.frame':   3 obs. of  2 variables:
   $ Char: int  1 2 3
   $ Num : int  11 12 13

Skip the call to cbind() and you will probably get what
you want.

Converting the character to a factor with the desired
order of levels is generally a good way to go.  Passing
that factor to cbind() is not.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  
> 
> last_plot() %+% x
> 
> HTH,
> 
> baptiste
> 
> On Thu, Jun 10, 2010 at 8:42 PM, Kim Jung Hwa 
> <kimhwamaillist at gmail.com> wrote:
> > Hello,
> >
> > I want to arrage the label according to my preference eg.. 
> (va, vp, a, b,
> > c) but don't know how to supress default ordering. Any
> > suggestions?
> >
> > Please try the code below:
> >
> > n <- c("va", "vp", "a", "b", "c")
> > p <- c(2, 2,1, 3,5)
> > pm<- c(3,4,2,5,4)
> > pn <- c(1,1,1,2,3)
> > x<-data.frame(cbind(n,p,pm,pn))
> > library(ggplot2)
> > qplot(x=n, y=p, data=x, ymin=pn, ymax=pm,
> >   xlab='', ylab='', main='Order Label as: va vp a b c') +
> >   geom_hline(yintercept = 2) +
> >   geom_linerange() +
> >   coord_flip()
> >
> > Thanks!
> >
> > --
> > You received this message because you are subscribed to the 
> ggplot2 mailing
> > list.
> > Please provide a reproducible example: http://gist.github.com/270442
> >
> > To post: email ggplot2 at googlegroups.com
> > To unsubscribe: email ggplot2+unsubscribe at googlegroups.com
> > More options: http://groups.google.com/group/ggplot2
> >
> 
> ______________________________________________
> R-help at 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.
> 



More information about the R-help mailing list