Based on this great question: How to draw a smooth curve passing through some points
How would one do this in lattice?
plot(rnorm(120), rnorm(120), col="darkblue", pch=16, xlim=c(-3,3), ylim=c(-4,4))
points(rnorm(120,-1,1), rnorm(120,2,1), col="darkred", pch=16)
points(c(-1,-1.5,-3), c(4,2,0), pch=3, cex=3)
xspline(c(-1,-1.5,-3), c(4,2,0), shape = -1)
Here is similar data, formatted more appropriately for a lattice
plot:
dat <- data.frame(x=c(rnorm(120), rnorm(120,-1,1)),
y=c(rnorm(120), rnorm(120,2,1)),
l=factor(rep(c('B','R'),each=120))
)
spl <- data.frame(x=c(-1,-1.5,-3),
y=c(4,2,0)
)
And here is what the linked question gave, translated to lattice
:
xyplot(y ~ x,
data=dat,
groups=l,
col=c("darkblue", "darkred"),
pch=16,
panel = function(x, y, ...) {
panel.xyplot(x=spl$x, y=spl$y, pch=3, cex=3)
## panel.spline(x=spl$x, y=spl$y) ## Gives an error, need at least four 'x' values
panel.superpose(x, y, ...,
panel.groups = function(x, y, ...) {
panel.xyplot(x, y, ...)
}
)
},
xlim=c(-3,3), ylim=c(-4,4)
)