[R] Obtaining lattice equivalent smoothed (loess) plot in ggplot

A Herath chaminal at hotmail.com
Wed Sep 29 23:48:36 CEST 2010



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).
Cham.
________________________________
> Date: Wed, 29 Sep 2010 14:19:42 -0700
> Subject: Re: [R] Obtaining lattice equivalent smoothed (loess) plot in ggplot
> From: djmuser at gmail.com
> To: chaminal at hotmail.com
> CC: r-help at r-project.org
>
> 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]]
>
> ______________________________________________
> 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