I have data that is set up like the following:
type age rust
0 1 29.00
0 0 95.00
.....
1 1800 7.56
I would like to create a scatter diagram of rust vs. age according to Type 0 and 1. I also would like to overlay a least square regression line in red for type 0 and blue for type 1.
I tried the following but when I tried overlaying the abline command it was way off the graph:
library(lattice)
xyplot(rust$rust~rust$age, group = rust$type, main = "Scatterplot of Rust vs. Age by Type")
abline(rust$rust~rust$age, col = "blue")
Elaborating on my comment, with your specific data, something like this should work.
require(lattice)
set.seed(42)
type <- rep(c(0,1),20)
age <- sample(1:100, 40, replace=TRUE)
rust <- type*5 + 0.3 * age + rnorm(40)
df <- data.frame(type = type, age = age, rust = rust)
myPanel <- function(x,y,...) {
panel.xyplot(x,y,...)
panel.abline(lm(y~x), ...)
}
xyplot(rust ~ age, group = type, data = df, panel = panel.superpose,
panel.groups = myPanel)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.