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

2008-06-03 Thread Paul Johnson
On Sat, May 31, 2008 at 2:45 PM, Peter Dalgaard
[EMAIL PROTECTED] 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

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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

2008-05-31 Thread Paul Johnson
Hello, everybody:

I recently encountered an example with  in which the graph was placed
in a way that did not leave room for a legend.  Maybe you would
describe it as legend too big, I'm not sure.  I found myself wishing
I could force in some space after the title.

Here's working example where I'd like to make room for a legend.



x - rnorm(100)

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(0,0.4, legend=c(observed density, normal with observed mean
 sd, normal with 'true' mean and sd), lty=c(1,2,3), col=c(black,
red, green))


I tried fiddling around with par to change the margins, but it has the
bad effect of resizing the image and creating a blank space into which
I'm not allowed to write the legend. Know what I mean? I try

par( mar=c(1,1,3,1)) or par(mai=c(1,1,4,1))

before the hist and it makes space, but useless space.

I've been considering something desperate, such as layout().  Isn't
there a simpler way?






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

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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

2008-05-31 Thread Peter Dalgaard

Paul Johnson wrote:

Hello, everybody:

I recently encountered an example with  in which the graph was placed
in a way that did not leave room for a legend.  Maybe you would
describe it as legend too big, I'm not sure.  I found myself wishing
I could force in some space after the title.

Here's working example where I'd like to make room for a legend.



x - rnorm(100)

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(0,0.4, legend=c(observed density, normal with observed mean
 sd, normal with 'true' mean and sd), lty=c(1,2,3), col=c(black,
red, green))


I tried fiddling around with par to change the margins, but it has the
bad effect of resizing the image and creating a blank space into which
I'm not allowed to write the legend. Know what I mean? I try

par( mar=c(1,1,3,1)) or par(mai=c(1,1,4,1))

before the hist and it makes space, but useless space.

I've been considering something desperate, such as layout().  Isn't
there a simpler way?

  

(1) par(xpd=TRUE) allows you to write outside the plotting region

(2) xlim and ylim can squeeze the  plot in suitable directions

Combined with what you are alread aware of, I think these should do the 
trick.



--
  O__   Peter Dalgaard Øster Farimagsgade 5, Entr.B
 c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen   Denmark  Ph:  (+45) 35327918
~~ - ([EMAIL PROTECTED])  FAX: (+45) 35327907

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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

2008-05-31 Thread Gabor Grothendieck
Is this ok?

set.seed(1)
# ... your code ...
legend(topright, title = Legend, legend=c(observed density,
normal with \nobserved mean\n sd, normal with \n'true' mean  sd),
lty = 1:3, col = 1:3, box.lty = 0, text.width = 1.1, cex = 0.7)


On Sat, May 31, 2008 at 3:06 PM, Paul Johnson [EMAIL PROTECTED] wrote:
 Hello, everybody:

 I recently encountered an example with  in which the graph was placed
 in a way that did not leave room for a legend.  Maybe you would
 describe it as legend too big, I'm not sure.  I found myself wishing
 I could force in some space after the title.

 Here's working example where I'd like to make room for a legend.



 x - rnorm(100)

 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(0,0.4, legend=c(observed density, normal with observed mean
  sd, normal with 'true' mean and sd), lty=c(1,2,3), col=c(black,
 red, green))


 I tried fiddling around with par to change the margins, but it has the
 bad effect of resizing the image and creating a blank space into which
 I'm not allowed to write the legend. Know what I mean? I try

 par( mar=c(1,1,3,1)) or par(mai=c(1,1,4,1))

 before the hist and it makes space, but useless space.

 I've been considering something desperate, such as layout().  Isn't
 there a simpler way?






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

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.