[R] How to add space between main title to leave space for legend?

Paul Johnson pauljohn32 at gmail.com
Wed Jun 4 02:50:23 CEST 2008


On Sat, May 31, 2008 at 2:45 PM, Peter Dalgaard
<p.dalgaard at biostat.ku.dk> wrote:
> Paul Johnson wrote:
>>
>> Hell
> (1) par(xpd=TRUE) allows you to write outside the plotting region
>
>  O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B

As usual, PD right on the money.  Here's a working demo program for
posterity.  For  "most" random samples, this gives a good looking
graph.  For a the other few, well, they happen less than 0.05 of the
time :)


### histogramWithDensityLinesAndLegend.R
### Paul Johnson 2008-06-02
### Thanks to r-help members for tips.


x <- rnorm(100)
###Allows writing outside plotting region

par(mai=c(1,1,2.5,1))


par(xpd=TRUE)

myhist <- hist(x, freq=F, main="Different Meanings of Normality")

lines(density(x))

xseq1 <- seq( min(x), max(x), length.out=100)

m1 <- mean(x)

sd1 <- sd(x)

obsNormal <- dnorm(xseq1, mean=m1, sd=sd1)

lines( xseq1, obsNormal, lty=2, col="red")

truNormal <- dnorm(xseq1)

lines(xseq1, truNormal, lty=3, col="green")

legend(min(xseq1),1.3*max(myhist$density), legend=c("observed
density", "normal with observed mean & sd", "normal with 'true' mean
and sd"), lty=c(1,2,3), col=c("black", "red", "blue"))





-- 
Paul E. Johnson
Professor, Political Science
1541 Lilac Lane, Room 504
University of Kansas



More information about the R-help mailing list