Skip to content

Densities in legend

2 messages · Daniel E. Ho, Uwe Ligges

#
Hello all,

Does anyone know how to add densities of a histogram to a legend?

I have tried to label five histograms with the following code, to no
avail -- each time, the legend symbol is simply blank.

postscript("graph9.eps")
title=c("Comparison of Quintiles")
hist(quant1$pre,xlim=c(260,310),ylim=c(0,12),ylab="Frequency",xlab="Cholesterol",density=10,main=title)
hist(quant2$pre,add=TRUE,density=20)
hist(quant3$pre,add=TRUE,density=30)
hist(quant4$pre,add=TRUE,density=40)
hist(quant5$pre,add=TRUE,density=50)
legend(260,12,density=c(10,20,30,40,50),legend=c("Quintile 1","Quintile 2","Quintile 3","Quintile 4","Quintile 5"))
dev.off()

Many thanks,

Dan

daniel_ho at harvard.edu



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Daniel En-Wenn Ho wrote:
The following will do the trick:
  legend(260, 12, fill = TRUE, angle = 45, density = 1:5 * 10, legend =
paste("Quantile", 1:5))

a) You need to specify "fill = TRUE" to draw boxes beside the legend
texts.
b) You need to specify an "angle" to plot shading lines.

Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._