Hi,

I want to get a histogram with the legend for my data. I drew a normal density curve and kernel density curve in the histogram, and I also label mean and median in the X axis. From the code, I got two legend: One shows "Normal Density" and "Kernel Density" and their corresponding lines, the other shows "Mean = value" and "Median = value" and their corresponding points. Actually I want to put them into one legend. I tried several ways but ended up with getting mixed line and point types.My code is as following:

hist(CDR3,xlab="",ylab="",main=NULL, xlim=c(min(CDR3),max(CDR3)),freq=FALSE,col="lightgrey")
# draw the overlaying normal curve and kernel curve, draw mean and median on X-axis
xfit<-seq(min(CDR3),max(CDR3),length=100)
yfit<-dnorm(xfit,mean=mean(CDR3),sd=sd(CDR3))
lines(xfit, yfit, col="red",lty=1,lwd=1)
lines(density(CDR3,from=min(CDR3),to=max(CDR3)),col="blue",lty=1,lwd=1) 
mean(CDR3)
median(CDR3)
points(x=mean(CDR3),y=0,pch=19,col="red",cex=1)
points(x=median(CDR3),y=0,pch=19,col="black",cex=1)
# here I got two legend
legend(x1,y1 c("Normal density","Kernel density"),col=c("red","blue"),lty=1:1,lwd=1:1,bty="n",)
legend(x2,y2 c(paste("Mean =",round(mean(CDR3),1)),paste("Median =",median(CDR3))),col=c("red","black"),pch=19:19,bty="n")

I tried the code below to create one legend, but I failed:
legend(x3,y3, c("Normal density","Kernel density","paste("Mean =",round(mean(CDR3),1)","paste("Median =",median(CDR3))"),c(lty=1,lty=1,pch=19,pch=19),col=c("red","blue","red","black"),bty="n")

Thank you,
Lu


      
	[[alternative HTML version deleted]]

