Hello, I have 2 variables - x and y, that belong to separate groups. I want to plot all the x and y together, but show separate abline for each group. It can be done in ggplot2, but is there a simpler way to draw ablines by group? e.g., mydata <- data.frame(x = 1:20+rnorm(20, -3, 1), y = seq(1,20,by=1), group rep(letters[1:5],20)) plot(x,y,col=mydata$group) # need to get separate ablines for each color-code [[alternative HTML version deleted]]
Hello, I have 2 variables - x and y, that belong to separate groups. I want to plot all the x and y together, but show separate abline for each group. It can be done in ggplot2, but is there a simpler way to draw ablines by group? e.g., mydata <- data.frame(x = 1:20+rnorm(20, -3, 1), y = seq(1,20,by=1), group rep(letters[1:5],20)) plot(x,y,col=mydata$group) # need to get separate ablines for each color-code [[alternative HTML version deleted]]
On 20.01.2012 16:38, Sam Chand wrote:> Hello, > I have 2 variables - x and y, that belong to separate groups. > I want to plot all the x and y together, but show separate abline for each > group. It can be done in ggplot2, but is there a simpler way to draw > ablines by group? > e.g., > > mydata<- data.frame(x = 1:20+rnorm(20, -3, 1), y = seq(1,20,by=1), group > rep(letters[1:5],20)) > plot(x,y,col=mydata$group) # need to get separate ablines for each > color-code1. The code does not run as is. 2. abline for more than two points is nonsense for your data, since the groups are not at all on one line. 3. So you may want separate regressions? Or a whole model of the data with different intercepts and same slope? Or in one model with different intercepts and different slopes? Hence we cannot help and for now I assume this is a homework problem and you have not even repeated the question properly. Uwe Ligges> > [[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.
I believe you are looking for analysis of covariance. I recommend the ancova function in the HH package. ## install.packages("HH") ## if needed library(HH) tmp <- ancova(y ~ x + group, data=mydata) tmp ## You suggested that you want the superpose panel. You can get ## that on a single page with the last page from update(attr(tmp, "trellis"), layout=c(1,1)) On Fri, Jan 20, 2012 at 10:38 AM, Sam Chand <sam.cs2010@gmail.com> wrote:> Hello, > I have 2 variables - x and y, that belong to separate groups. > I want to plot all the x and y together, but show separate abline for each > group. It can be done in ggplot2, but is there a simpler way to draw > ablines by group? > e.g., > > mydata <- data.frame(x = 1:20+rnorm(20, -3, 1), y = seq(1,20,by=1), group > rep(letters[1:5],20)) > plot(x,y,col=mydata$group) # need to get separate ablines for each > color-code > > [[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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]