[R] Lattice: Plotting two densities on the same plot(s)?
Olivia Lau
olau at fas.harvard.edu
Wed Jan 8 04:46:02 CET 2003
I am trying to plot two density lines on the same graph. Using the
functions on the base package, I would go:
plot(density(x), col = 1)
lines(density(y), col = 2)
And I get two distinct (one-bump) density lines. When I try to do it
using lattice, I get two two-humped lines. (In other words, I think the
smoothing function is taking the next set of data points and smoothing them
in the same function as the prior set.)
Using:
library(nlme)
library(grid)
Let:
democrat <- rnorm(100, 0.3, 0.1)
republican <- rnorm(100, 0.5, 0.1)
state <- c("Delaware")
temp1 <- as.data.frame(cbind(state, democrat, republican))
democrat <- rnorm(100, 0.5, 0.1)
republican <- rnorm(100, 0.7, 0.1)
state <- c("Pennsylvania")
temp2 <- as.data.frame(cbind(state, democrat, republican))
data1 <- rbind(temp1, temp2)
What I'm doing right now is:
densityplot(~ democrat | state, data = data1,
xlim = c(0, 1), ylim = c(0, 10),
panel = function(x) {
panel.densityplot(republican, col = "red")
panel.densityplot(democrat, col = "black")
})
What should I do to draw two separate density plots on the same lattice
device?
Thanks,
Olivia Lau
More information about the R-help
mailing list