Naresh Gurbuxani
2015-Oct-04 19:30 UTC
[R] lattice plot: points and lines for different variables in same plotlattice plot
I want to draw scatter plot and a fitted line in the same lattice plot. My problem is that either both can be plotted as points or both as lines. The code that attempts to plot points for data and lines for fitted line does not work. Thanks, Naresh library(lattice) library(plyr) my.df <- data.frame(x = rnorm(100), y = rnorm(100), name = "A") temp.df <- data.frame(x = rnorm(100), name = "B") temp.df <- within(temp.df, {y <- x + 0.2 * rnorm(100)}) my.df <- rbind(my.df, temp.df) temp.df <- data.frame(x = rnorm(100), name = "C") temp.df <- within(temp.df, {y <- x + 0.5 * x^2 + 0.2 * rnorm(100)}) my.df <- rbind(my.df, temp.df) my.df <- ddply(my.df, c("name"), mutate, y.fit = lm(y ~ x + I(x^2))$fitted.values) my.df <- my.df[order(my.df$name, my.df$x),] # This works xyplot(y + y.fit ~ x | name, data = my.df, type = c("l")) # This does not work. Line plot seems wrong. xyplot(y + y.fit ~ x | name, data = my.df, type = c("l"), y.prime = my.df$y.fit, panel = function(x, y, y.prime, ...){panel.xyplot(x, y); panel.lines(x, y.prime, type = "l")}) [[alternative HTML version deleted]]
Duncan Mackay
2015-Oct-05 02:55 UTC
[R] lattice plot: points and lines for different variables in same plotlattice plot
Hi Naresh Try xyplot(y + y.fit ~ x | name, data = my.df, type = c("p","l"), distribute.type = TRUE, panel = panel.superpose ) Your code seems to be a direct copy from the command line; sometimes it makes it clearer to arguments and functions within the panel function on their own line Regards Duncan Duncan Mackay Department of Agronomy and Soil Science University of New England Armidale NSW 2351 Email: home: mackay at northnet.com.au -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Naresh Gurbuxani Sent: Monday, 5 October 2015 05:30 To: r-help at r-project.org Subject: [R] lattice plot: points and lines for different variables in same plotlattice plot I want to draw scatter plot and a fitted line in the same lattice plot. My problem is that either both can be plotted as points or both as lines. The code that attempts to plot points for data and lines for fitted line does not work. Thanks, Naresh library(lattice) library(plyr) my.df <- data.frame(x = rnorm(100), y = rnorm(100), name = "A") temp.df <- data.frame(x = rnorm(100), name = "B") temp.df <- within(temp.df, {y <- x + 0.2 * rnorm(100)}) my.df <- rbind(my.df, temp.df) temp.df <- data.frame(x = rnorm(100), name = "C") temp.df <- within(temp.df, {y <- x + 0.5 * x^2 + 0.2 * rnorm(100)}) my.df <- rbind(my.df, temp.df) my.df <- ddply(my.df, c("name"), mutate, y.fit = lm(y ~ x + I(x^2))$fitted.values) my.df <- my.df[order(my.df$name, my.df$x),] # This works xyplot(y + y.fit ~ x | name, data = my.df, type = c("l")) # This does not work. Line plot seems wrong. xyplot(y + y.fit ~ x | name, data = my.df, type = c("l"), y.prime my.df$y.fit, panel = function(x, y, y.prime, ...){panel.xyplot(x, y); panel.lines(x, y.prime, type = "l")}) [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.