[R] Overplotting: plot() invocation looks ugly ... suggestions?

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jul 27 02:53:57 CEST 2006


With the lattice package it would be done like this (where
the panel.points function places large red pluses on
the plot):

xyplot(Consumption ~ Quarter, group = Year, data, type = "o")
trellis.focus("panel", 1, 1)
panel.points(1:4, mean.per.quarter, pch = "+", cex = 2, col = "red")
trellis.unfocus()


On 7/26/06, John McHenry <john_d_mchenry at yahoo.com> wrote:
> Hi Hadley,
>
> Thanks for your suggestion.
>
> The description of ggplot states:
>
> Description:   ... It combines the advantages of both base and lattice
>                graphics ... and you can still build up a plot step by
>               step from multiple data sources
>
> So I thought I'd try to enhance the plot by adding in the means from each quarter (this is snagged directly from ESS):
>
> >   qplot(Quarter, Consumption, data=data, type=c("point","line"), id=data$Year)
>    ( mean.per.quarter<- with(data, tapply(Consumption, Quarter, mean)) )
>    points(mean.per.quarter, pch="+", cex=2.0)
>
>  qplot(Quarter, Consumption, data=data, type=c("point","line"), id=data$Year)
> >  ( mean.per.quarter<- with(data, tapply(Consumption, Quarter, mean)) )
>    1     2     3     4
> 888.2 709.2 616.4 832.8
> >  points(mean.per.quarter, pch="+", cex=2.0)
> Error in plot.xy(xy.coords(x, y), type = type, ...) :
>    plot.new has not been called yet
> >
> >
>
> Now I'm green behind the ears when it comes to R, so I'm guessing that there is some major conflict between base graphics and lattice graphics, which I thought ggplot avoided, given the library help blurb.
>
> I'm assuming that there must be a way to add points / lines to lattice / ggplot graphics (in the latter case it seems to be via ggpoint, or some such)? But is there a way that allows me to add via:
>
> points(mean.per.quarter, pch="+", cex=2.0)
>
> and similar, or do I have to learn the lingo for lattice / ggplot?
>
> Thanks,
>
> Jack.
>
>
>
> hadley wickham <h.wickham at gmail.com> wrote: > And if lattice is ok then try this:
> >
> > library(lattice)
> > xyplot(Consumption ~ Quarter, group = Year, data, type = "o")
>
> Or you can use ggplot:
>
> install.packages("ggplot")
> library(ggplot)
> qplot(Quarter, Consumption, data=data,type=c("point","line"), id=data$Year)
>
> Unfortunately this has uncovered a couple of small bugs for me to fix
> (no automatic legend, and have to specify the data frame explicitly)
>
> The slighly more verbose example below shows you what it should look like.
>
> data$Year <- factor(data$Year)
> p <- ggplot(data, aes=list(x=Quarter, y=Consumption, id=Year, colour=Year))
> ggline(ggpoint(p), size=2)
>
> Regards,
>
> Hadley
>
>
>
> ---------------------------------
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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