Here's my cooked up example:

# Faked data
x <- sample(1:100, 300, replace = TRUE)
#  y = a + bx + cx^2 + noise, where a, b, c differ in each group
y <- rep(c(2, 5, 3), each = 100) + rep(c(-0.5, 0.5, 1), each = 100) * x +
     rep(c(0.01, -0.01, 0.02), each = 100) * x^2 + rnorm(300, 0, 10)
g <- rep(LETTERS[1:3], each = 100)
dd <- data.frame(x, y, g)
xyplot(y ~ x, data = dd, groups = g)

# Groupwise loess plots in lattice:
with(dd, xyplot(y ~ x, lty = 1, col.line = 1:3, col = 1:3, subscripts =
TRUE,
                panel = function(x, y, ..., groups) {
                   panel.superpose(x, y, ..., groups = g,
                                    panel = panel.points)
                   panel.superpose(x, y, ..., groups = g,
                                    panel = panel.loess) })
    )

# The same in ggplot2:
g <- ggplot(dd, aes(x = x, y = y, group = g))
g + geom_point(aes(colour = g)) +
    geom_smooth(aes(colour = g), size = 1, se = FALSE) +
    scale_colour_hue('Group')

# ggplot can also provide SE envelopes if you take out the se = FALSE
snippet:
g + geom_point(aes(colour = g)) +
    geom_smooth(aes(colour = g), size = 1) +
    scale_colour_hue('Group')

HTH,
Dennis

On Wed, Sep 29, 2010 at 2:48 PM, A Herath <[email protected]> wrote:

>
>
> Hello,
> My apologies, it was the hotmail writer which ate my post (hopefully this
> will get there, intact).
>
> dd<-rbind(data.frame(rbind(c("V1","A",0.3),c("V2","A",0.5),c("V3","A",0.2))),
>    data.frame(rbind(c("V1","B",0.3),c("V2","B",0.4),c("V3","B",0.8))),
> data.frame(rbind(c("V1","C",0.9),c("V2","C",0.2),c("V3","C",0.4))))
> require(lattice)
> # Lattice Code -- this is the behaviour that I need to replicate in ggplot#
> No it is not a home work assignment, I did home work about 25 years ago#
> This is for my work, I would like the aesthetics of ggplot :)#xyplot(X3 ~
> X1, data=dd, type="l", group=X2,      panel = function(x,y,...)  {
> panel.xyplot(x,y,...)                   panel.loess(x,y,lty=2,lwd=1,...)
>    })
> # This is the ggplot(2) code, it probably does what it is supposed to i.e.
> group# but not the behaviour shown by the lattice code#
> p <- ggplot(dd, aes(x=X1,y=X3,group=X2))p <-p + geom_smooth(method =
> "loess",se=FALSE, lty=2,lwd=1)p + geom_line(aes(col=X2))
>
> I have the plot above in Lattice, but would like to get a ggplot2 plot for
> a presentation (i.e. it just looks nice). (I am not sure whether somebody
> would set something like this for a home work assignment).
>

You'd be surprised what they do in graduate school these days...


> Cham.
> ________________________________
> > Date: Wed, 29 Sep 2010 14:19:42 -0700
> > Subject: Re: [R] Obtaining lattice equivalent smoothed (loess) plot in
> ggplot
> > From: [email protected]
> > To: [email protected]
> > CC: [email protected]
> >
> > Hi:
> >
> > There's no way you could produce a loess plot based on the data
> > supplied below. V1, your purported x-variable, is a factor; moreover,
> > you have one point per V1 * V2 factor combination. (BTW, you might also
> > consider using the carriage return when demarcating individual lines of
> > code.)
> >
> > The reason I know your loess code wouldn't work in lattice is because
> > panel.loess() does not react to the groups argument. I learned this
> > from Dr. Sarkar a month or so ago; I have code that does work, but it
> > requires two calls to panel.superpose inside a panel function.
> >
> > I have a worked example for loess with both lattice and ggplot2 in
> > multiple groups, but given the code you provided, I'm seriously
> > wondering if this is a homework assignment, which is why I've demurred
> > in supplying it. If this pertains to a homework assignment, please read
> > the Posting Guide; if not, state your case.
> >
> > I might also mention that you're mixing in graphics parameters from
> > base graphics into ggplot2. That doesn't work, either.
> > lty is linetype in ggplot2, lwd is size...
> >
> > The on-line help for ggplot2 is here:
> > http://had.co.nz/ggplot2/
> >
> > Dennis
> >
> > On Wed, Sep 29, 2010 at 12:58 PM, A Herath
> >> wrote:
> >
> > Hello,
> > I have been struggling to do a plot in ggplot(2) that's of lattice
> > equivalent. The following code shows the lattice plot.
> >
> dd<-rbind(data.frame(rbind(c("V1","A",0.3),c("V2","A",0.5),c("V3","A",0.2))),data.frame(rbind(c("V1","B",0.3),c("V2","B",0.4),c("V3","B",0.8))),data.frame(rbind(c("V1","C",0.9),c("V2","C",0.2),c("V3","C",0.4))))
> >
> > require(lattice)
> > xyplot(X3 ~ X1, data=dd, type="l", group=X2, panel =
> > function(x,y,...) { panel.xyplot(x,y,...)
> > panel.loess(x,y,lty=2,lwd=1,...) })
> > I would like to obtain the same result in ggplot(2), but the following
> > does not give the equivalent above. I wonder whether anybody can help?
> > require(ggplot2)
> > p <- ggplot(dd, aes(x=X1,y=X3,group=X2))p <-p + geom_smooth(method =
> > "loess",se=FALSE, lty=2,lwd=1)p + geom_line(aes(col=X2))
> > Many Thanks,
> > Cham
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [email protected] 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.
> >
>
>

        [[alternative HTML version deleted]]

______________________________________________
[email protected] 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.

Reply via email to