A Herath
2010-Sep-29 19:58 UTC
[R] Obtaining lattice equivalent smoothed (loess) plot in ggplot
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]]
Dennis Murphy
2010-Sep-29 21:19 UTC
[R] Obtaining lattice equivalent smoothed (loess) plot in ggplot
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 <chaminal@hotmail.com> 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@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. >[[alternative HTML version deleted]]