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.