0

My problem is with the legend. The legend is cut off, so it is missing two values. How do I go about moving legends to the title position, while still maintaining correct formating so that everything fits and is aligned to the plot?

My legends are of varying lengths so it would be great to have a way to always have them perfectly line up above the plot.

x<-c(1, 30,60) 
y<-c(.001,.023,.03)
data<-cbind(x,y)
N<-100    

       plot_pdf_1<-function(data, ymax, big){
           par(cex=big)
           plot(data, type = "l", lwd=2,xaxt="n", yaxt="n", xaxs="i", yaxs="i", ylim=c(0,ymax))
           my_at<- c(0,10,20,30,40,50,60,70,80,90) # to specify the tick marks    

  #initialize all points to zero first
  #number of data points
  N_o1_male<-0
  N_o1_female<-0
  N_o139_male<-0 
  N_o139_female<-0
  N_non_male<-0
  N_non_female<-0 

  #subscript format to be used in my legend
  my.expressions <-c(as.expression(bquote('N'['1M']*' = '*.( N_o1_male))), as.expression(bquote('N'['1F']*' = '*.( N_o1_female))), as.expression(bquote('N'['2M']*' = '*.( N_o139_male))),as.expression(bquote('N'['2F']*' = '*.( N_o139_female))), as.expression(bquote('N'['3M']*' = '*.( N_non_male))),
    as.expression(bquote('N'['3F']*' = '*.( N_non_female))))  

  par(xpd=TRUE)#to allow legend  in outer margins




  legend("topleft",legend=my.expressions,inset=c(0,-.11),
  text.col="black",box.col=0, bty="n", cex = .75, lty= c( 1,2,1,2,1,2), col = c("purple","purple","blue","blue","black","black"),horiz = TRUE,seg.len = 1)
       }



        #formatting to plot in a two by two layout
        op <- par(mfrow = c(2,2), 
              oma = c(5,4,0,0) + 0.1,
              mar = c(0,0,1,.5) + 0.1)

        #calls each plotting function and layout in a two by two
        twobytwo<-function(data,ymax,big){
          op 
          plot_pdf_1(data,ymax,big)
          plot_pdf_1(data,ymax,big)
          plot_pdf_1(data,ymax,big)
          plot_pdf_1(data,ymax,big)

          title(xlab = "Age (years)",
            ylab = "Probability Density",
            outer = TRUE, line = 3)}


        twobytwo(data, ymax=.04, big=1) #calls  the two by two function which lays out four plots in a two by two format. The plots share the same axis.
Meli
  • 345
  • 5
  • 15
  • you can specify the x/y values in `legend(par('usr')[1], par('usr')[4] + .01, ...)` – rawr Dec 16 '15 at 00:04

1 Answers1

0

Three solutions:

  1. Reduce font with cex
  2. Remove the spaces on both sides of the ' = '
  3. Widen your chart. You can set the chart size with win.graph() in
    Windows or X11() or quartz() in other OS.

Using win.graph(width=11, h=7) before the op<-... call

enter image description here

Pierre Lapointe
  • 16,017
  • 2
  • 43
  • 56
  • @P Lapointe Is there anyway to do this using 'mtext()'. I'm finding that when I knit to HTML or Word some of the images work perfectly like above and others do not and have parts of the legend overlapping. – Meli Dec 17 '15 at 17:48
  • Unfortunately, I don't think you can do that with `mtext` You will not have the lines identifiers. There are two more things you could try: [See my answer here](http://stackoverflow.com/questions/8929663/r-legend-placement-in-a-plot/8930448#8930448) to make sure that the legend does not overlap with data. Also, you could put your legend inside on two columns with `ncol=2` in a blank space. – Pierre Lapointe Dec 17 '15 at 18:15