Hi,
Sorry to forget providing the data before.
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:
CDR3
<- c(16,12, 15, 12, 14, 20, 15, 14, 12, 16, 16, 16, 14, 14, 14, 14,
14, 14, 19, 19, 19, 19, 19, 19, 16, 16, 14, 16, 14, 13, 10, 20, 18, 14,
17, 13, 14, 11, 14, 18,
17, 20, 22, 19, 13, 21, 19, 22, 11, 14, 22, 17, 17, 21, 11, 11, 20, 18, 13, 10,
13, 11, 12, 10, 9, 9, 12, 12, 11, 10, 14,
12, 12, 15, 14, 12, 23, 10, 20, 13, 39, 23, 23, 13, 12, 14, 14, 14, 14, 12, 12,
10)
par(mar=c(4,4,4,4),oma=c(0,0,0,0))
hist(CDR3,xlab="",ylab="",main=NULL,
xlim=c(min(CDR3),max(CDR3)),freq=FALSE,col="lightgrey")
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)
CDR3_mean=mean(CDR3)
CDR3_median=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)
legend(30,0.08, c("Normal density","Kernel
density"),col=c("red","blue"),lty=1:1,lwd=1:1,bty="n")
legend(30,0.06, 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]]