将图例作为标题以二乘二的方式绘制在R中的基本图布局中

将图例作为标题以二乘二的方式绘制在R中的基本图布局中,r,plot,legend,R,Plot,Legend,我的问题在于传说。图例被截断,因此缺少两个值。如何将图例移动到标题位置,同时仍保持正确的格式,以便所有内容都适合并与情节对齐 我的传奇故事的长度各不相同,所以如果有办法让它们在情节上方完美地排列,那就太好了 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=b

我的问题在于传说。图例被截断,因此缺少两个值。如何将图例移动到标题位置,同时仍保持正确的格式,以便所有内容都适合并与情节对齐

我的传奇故事的长度各不相同,所以如果有办法让它们在情节上方完美地排列,那就太好了

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.
x三种解决方案:

  • 使用
    cex减少字体
  • 删除“=”两侧的空格
  • 扩大你的图表。您可以在
    Windows或其他操作系统中的X11()或quartz()

  • 使用
    win.graph(width=11,h=7)
    操作之前,您可以在
    图例(par('usr')[1],par('usr')[4]+.01,…)中指定x/y值。
    @p Lapointe是否可以使用“mtext()”执行此操作。我发现,当我编织HTML或Word时,一些图像的工作方式与上面的完全相同,而其他图像则没有,并且图例部分重叠。不幸的是,我认为使用
    mtext
    无法做到这一点,因为您将没有行标识符。您还可以尝试两件事:确保图例不会与数据重叠。此外,您还可以将图例放在两列中,并在空白处显示
    ncol=2