Blogger

Delete comment from: One R Tip A Day

mhc said...

I have run the following command to estimate the density function with legend. But it estimate the density function without legend. why so? could you please answer?

setwd("c:/mydata")
getwd()
setwd("c:/mydata")
getwd()
data1<-read.table("MPI_NEW.csv",header=T,sep=",")
attach(data1)
names(data1)
[1] "sn" "MPI_03_04" "MPI_04_05" "MPI_05_06"

plot.multi.dens <- function(s)
{
junk.x = NULL
junk.y = NULL
for(i in 1:length(s))
{
junk.x = c(junk.x, density(s[[i]])$x)
junk.y = c(junk.y, density(s[[i]])$y)
}
xr <- range(junk.x)
yr <- range(junk.y)
plot(density(s[[1]]), xlim = xr, ylim = yr, xlab=" ", main = "MPI")
for(i in 1:length(s))
{
lines(density(s[[i]]), xlim = xr, ylim = yr, col = i)
}
}
x=t(matrix(c(data1$MPI_03_04)))
y=t(matrix(c(data1$MPI_04_05)))
z=t(matrix(c(data1$MPI_05_06)))
plot.multi.dens(list(MPI_03_04,MPI_04_05,MPI_05_06)
legend(5.8,0.35,legend=c("x","y","z"), col=c(1:3), lty=1)

Jun 19, 2009, 10:43:03 PM


Posted to Plotting two or more overlapping density plots on the same graph

Google apps
Main menu