Plotting densities in R
So, I am pl开发者_开发知识库otting densities (histograms). For example:
d <- density(table[table$position==2,]$rt)
But, I want to plot multiple densities on the same plot. For instance, I also want to plot
density(table[table$position==3,]$rt)
density(table[table$position==4,]$rt)
density(table[table$position==5,]$rt)
Furthermore, I want to specify the center point for each of these densities.
Another way to ask this question is, how can I manually shift a density plot over by a certain number of x units? (for instance, increase all x values by 5)
As with many R analysis functions, saving the output is your friend. So is ?density
.
foo<-density(something)
names(foo)
"x", "y" , "bw", "n" , "call" ,"data.name"
So,
plot(foo$x+5, foo$y, t='l')
And you're done so far as I can tell.
For the piece of your question about plotting multiple densities on the same plot, use lines
:
dat <- data.frame(x = rnorm(100), y = rnorm(100) + 2, z = rnorm(100) + 5)
plot(c(-2.5,8),c(0,0.5),type = "n")
lines(density(dat$x))
lines(density(dat$y))
lines(density(dat$z))
You open an empty plotting device using plot(...,type = "n")
and then draw on it using lines
or points
, etc.
精彩评论