[R] ggplot - Setting the y-scale in a bar plot

hadley wickham h.wickham at gmail.com
Thu Dec 13 15:52:02 CET 2007


> PS: If you care to explain: why do all parameters in the code below
> have a "." before the name, except precisely "limits"? I know it has
> to do with "proto", but could not find out why this one was different.

Not really - I started off with the convention that variables should
start with a . to distinguish them from functions of the same name.
Unfortunately I forgot about the convention later on, so the code is a
bit confusing!

(And sorry for the confusion of limit vs. limits)

Hadley

> =================
> [...] The fix will be included in the next version of
> ggplot, or you can fix the current version by running this code:
>
> ScaleContinuous$new <- function(., name=NULL, limits=c(NA,NA),
> breaks=NULL, labels=NULL, variable, trans="identity", expand=c(0.05,
> 0)) {
>      if (is.null(breaks) && !is.null(labels)) stop("Labels can only be
> specified in conjunction with breaks")
>
>      .$proto(name=name, .input=variable, .output=variable,
> limits=limits, .breaks = breaks, .labels = labels, .expand=expand)
>    }
>
> ==============================================================
> At 23:01 2007/12/11, you wrote:
> >Hi Pedro,
> >
> >It seems to work for me:
> >
> >qplot(mpg, wt, data=mtcars)
> >qplot(mpg, wt, data=mtcars) + scale_y_continuous(limit=c(4,5))
> >
> >Maybe I don't understand what you're trying to do.
> >
> >Hadley
> >
> >On 12/11/07, Pedro de Barros <pbarros at ualg.pt> wrote:
> > > Hi Hadley,
> > >
> > > Well, the problem seems to be that ggplot is not recognizing the
> > > scale when set by scale_y_continuous, the maximum value stays exactly
> > > at the data range, irrespective of what I provide as range.
> > > I am not familiar yet with proto, therefore I do have some difficulty
> > > delving into the code to find out exactly what is wrong, but I hope
> > > you can tell if I am doing something pretty stupid, or f it is a
> > > feature or a bug...
> > >
> > > Cheers,
> > > Pedro
> > > At 21:15 2007/12/11, you wrote:
> > > >Hi Pedro,
> > > >
> > > >What's the problem exactly?  You'll need to compute the range
> > > >yourself, and then use scale_y_continuous as you have below.
> > > >
> > > >(Also you can abbreviate the bar chart plotting command to:
> > > >qplot(x, y, data=plotdata, geom="bar", stat="identity"))
> > > >
> > > >Hadley
> > > >
> > > >On 12/11/07, Pedro de Barros <pbarros at ualg.pt> wrote:
> > > > > Dear All (probably Hadley),
> > > > >
> > > > > I am now trying to customise some plots using a bar geom.
> > > > >
> > > > > I do not want to use the default binning statistic, but rather
> > > > > calculate the bar heigths separately. I do manage this, but for
> > > > > comparison purposes I would like to have a set of plots all with the
> > > > > same y-axis height. But I do not seem to find out how to fix the
> > > > > scale of the y-axis in this case.
> > > > > Any tips?
> > > > > Using R 2.6.1 on Windows.
> > > > >
> > > > > Thanks for any help,
> > > > > Pedro
> > > > >
> > > > > I attach below the code I am using:
> > > > > plotdata<-data.frame(x=factor(2:8), y=0.1*(2:8))
> > > > > plot1<-ggplot()
> > > > > plot1<-plot1+layer(data=plotdata,
> > > > > mapping=aes_string(x='x',y='y'),geom='bar', stat='identity')
> > > > >
> > > > > RangeY <-c(0,1)
> > > > > YBreaks <- (0:10)*diff(RangeY)/10
> > > > > YTickLabels<- as.character(YBreaks)
> > > > >
> > > > > plot2 <- plot1 + scale_y_continuous(limits=RangeY, breaks=YBreaks,
> > > > > labels=YTickLabels, expand=c(0,0))
> > > > > print(plot2)
> > > > >
> > > > > ______________________________________________
> > > > > 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.
> > > > >
> > > >
> > > >
> > > >--
> > > >http://had.co.nz/
> > >
> > >
> >
> >
> >--
> >http://had.co.nz/
>
>


-- 
http://had.co.nz/



More information about the R-help mailing list