如何将图例添加到多图图中?

时间:2018-09-24 18:26:59

标签: r plot legend

以下代码已从各种海报中收集到。我只是想在R中添加一个简单的图例,发现它变得非常复杂。创建后,Model1和Model2的文本相距很远。我用以下几行代码解决了这个问题:

plot_colors <- c("blue","red")
text <- c("Model1", "Model2")
xcoords <- c(0, 1.5)
secondvector <- (1:length(text))-1
textwidths <- xcoords/secondvector 
textwidths[1] <- 0 

,但是包含图例的框太小。我以为我解决了这个问题,首先用plot=FALSE创建了另一个图例(腿),下面是这些行(只是为了得到盒子的x,y坐标):

leg<-legend(x=-4, y=-240, legend=text, lty=1, cex=1.0, ncol=2, xpd=NA,col=c("blue", "red"),
       lwd=c(3,3),text.width=textwidths,plot=FALSE)
leftx <- leg$rect$left
rightx <- (leg$rect$left + leg$rect$w) * 1.2
topy <- leg$rect$top
bottomy <- (leg$rect$top - leg$rect$h) * 1.2
legend(x = c(leftx, rightx), y = c(topy, bottomy), legend=text, lty=1,
   cex=1.0, ncol=2, xpd=NA,col=c("blue", "red"),
   lwd=c(3,3),text.width=textwidths)

但是现在我收到此错误,不确定是什么意思。

  

xy.coords(x,y,setLab = FALSE)错误:'x'和'y'的长度不同

完整代码如下:

#Error Analysis
DRP_1<-c(   3   ,   0   ,   -2  ,   NA  ,   NA  ,   -3  ,   -3  ,   -7  ,   3   ,   3   ,   0   ,   -6  )
Forecast_1<-c(  2   ,   2   ,   2   ,   11  ,   16  ,   16  ,   17  ,   16  ,   27  ,   27  ,   26  ,   22  )
DRP_2<-c(   -3  ,   -3  ,   -1  ,   NA  ,   NA  ,   6   ,   9   ,   16  ,   13  ,   10  ,   10  ,   16  )
Forecast_2<-c(  -2  ,   -4  ,   -6  ,   -17 ,   -33 ,   -49 ,   -66 ,   -82 ,   -109    ,   -136    ,   -162    ,   -184    )

DRP_1_AprMay<-c(    NA  ,   NA  ,   -2  ,   -2  ,   -3  ,   -3  ,   NA  ,   NA  ,   NA  ,   NA  ,   NA  ,   NA  )
DRP_2_AprMay<-c(    NA  ,   NA  ,   -1  ,   -1  ,   6   ,   6   ,   NA  ,   NA  ,   NA  ,   NA  ,   NA  ,   NA  )

par(mfrow=c(1,2))
par(mar=c(6,3,0,0.5))
par(mgp=c(2,0.25,0))
par(oma=c(0,1,3,0))
#
# CFE for Part 3315
#
g_range <- range(-7, DRP_1, Forecast_1,na.rm = TRUE)

plot(DRP_1, type="o", col="blue", ylim=c(-10,30), 
     axes=FALSE, ann=FALSE,lty=1)
axis(1, at=1:12, lab=c("Jan","","Mar","","May","","Jul","","Sep","","Nov",""))

axis(2, las=1,at=c(-10,0,10,20,30))

box()

lines(Forecast_1, type="o", pch=22, lty=1, col="red")

#Apr-May dashed lines

lines(DRP_1_AprMay,lty=2, col="blue")


abline(h = c(-10,0,10,20,30), lty = 2, col = "grey")
abline(v = 1:12,  lty = 2, col = "grey")
#abline(h = 0, lty = 1,lwd=1.5, col = "darkgrey")

title(xlab="Month in 2017", col.lab=rgb(0,0,0))
title(ylab="Cumuative Forecast Error (Number of Parts)", col.lab=rgb(0,0,0))

#
# PIS for Part 3315
#
g_range <- range(-184, DRP_2, Forecast_2,na.rm = TRUE)

plot(DRP_2, type="o", col="blue", ylim=c(-200,20), 
     axes=FALSE, ann=FALSE,lty=1)
axis(1, at=1:12, lab=c("Jan","","Mar","","May","","Jul","","Sep","","Nov",""))

axis(2, las=1,at=c(-200,-180,-160,-140,-120,-100,-80,-60,-40,-20,0,20))

box()
lines(Forecast_2, type="o", pch=22, lty=1, col="red")
#Apr-May dashed lines 

lines(DRP_2_AprMay,lty=2, col="blue")

abline(h = c(-200,-180,-160,-140,-120,-100,-80,-60,-40,-20,0,20), lty = 2, col = "grey")
abline(v = 1:12,  lty = 2, col = "grey")
#abline(h = 0, lty = 1,lwd=1.5, col = "darkgrey")
title(xlab="Month in 2017", col.lab=rgb(0,0,0))
title(ylab="Periods in Stock (Part-Months)", col.lab=rgb(0,0,0))

plot_colors <- c("blue","red")
text <- c("Model1", "Model2")
xcoords <- c(0, 1.5)
secondvector <- (1:length(text))-1
textwidths <- xcoords/secondvector 
textwidths[1] <- 0 
leg<-legend(x=-4, y=-240, legend=text, lty=1, cex=1.0, ncol=2, xpd=NA,col=c("blue", "red"),
       lwd=c(3,3),text.width=textwidths,plot=FALSE)
leftx <- leg$rect$left
rightx <- (leg$rect$left + leg$rect$w) * 1.2
topy <- leg$rect$top
bottomy <- (leg$rect$top - leg$rect$h) * 1.2
legend(x = c(leftx, rightx), y = c(topy, bottomy), legend=text, lty=1,
       cex=1.0, ncol=2, xpd=NA,col=c("blue", "red"),
       lwd=c(3,3),text.width=textwidths)
mtext(" Part 3315",outer=TRUE,side=3,cex=1.2,line=1)

1 个答案:

答案 0 :(得分:1)

您收到的错误消息与以下事实有关:您在x =调用中向y =传递了4个数字,向legend()传递了两个数字。

您可以通过仅使用leftxrightx中的一个元素来解决此问题:

legend(x = c(leftx[1], rightx[2]), y = c(topy, bottomy), legend=text, lty=1,
   cex=1.0, ncol=2, xpd=NA,col=c("blue", "red"),
   lwd=c(3,3), text.width=textwidths
)

但是,如果您想在此处放置图例,可能更干净的方法是在一个帧中重新捕获整个绘图区域:

par(fig=c(0, 1, 0, 1), oma=c(0, 0, 0, 0), mar=c(0, 0, 0, 0), new=TRUE)
plot.new()
legend("bottom", legend=text, lty=1, cex=1, ncol=2, xpd=TRUE, col=c("blue", "red"), lwd=3, bty="n")

结果如下:

picture

在这种情况下,您将不需要所有这些leftxrightx变量。

我还注意到您没有使用某些可能对您的情况有用的功能。例如,grid()将灰色网格线添加到绘图中。修改后的版本的完整代码可能如下所示:

par(mfrow=c(1,2), mar=c(6,3,0,0.5), mgp=c(2,0.25,0), oma=c(0,1,3,0))

# plot 1
plot.new()
plot.window(xlim=c(1, length(DRP_1)), ylim=c(-10,30))
grid(nx=length(DRP_1), ny=NULL)
box()
lines(DRP_1, type="o", col="blue")
lines(Forecast_1, type="o", pch=22, lty=1, col="red")
lines(DRP_1_AprMay,lty=2, col="blue")
axis(1, at=1:12, lab=c("Jan","","Mar","","May","","Jul","","Sep","","Nov",""), tck=-0.01)
axis(2, las=1, tck=-0.01)

title(xlab="Month in 2017", col.lab=rgb(0,0,0))
title(ylab="Cumuative Forecast Error (Number of Parts)", col.lab=rgb(0,0,0))

# plot 2
plot.new()
plot.window(xlim=c(1, length(DRP_2)), ylim=c(-200,20))
grid(nx=length(DRP_2), ny=NULL)
box()
lines(DRP_2, type="o", col="blue")
lines(Forecast_2, type="o", pch=22, lty=1, col="red")
lines(DRP_2_AprMay,lty=2, col="blue")
axis(1, at=1:12, lab=c("Jan","","Mar","","May","","Jul","","Sep","","Nov",""), tck=-0.01)
axis(2, las=1, tck=-0.01)

title(xlab="Month in 2017", col.lab=rgb(0,0,0))
title(ylab="Periods in Stock (Part-Months)", col.lab=rgb(0,0,0))

# legend
par(fig=c(0,1,0,1), oma=c(0,0,0,0), mar=c(0,0,0,0), new=TRUE)
plot.new()
legend("bottom", legend=c("Model1", "Model2"), lty=1, cex=1, ncol=2, xpd=TRUE, col=c("blue", "red"), lwd=3, bty="n")

具有以下结果:

picture2