在图的顶部添加图例

时间:2013-06-27 06:37:57

标签: r plot legend

我的传奇故事在我的情节中出现了。为了解决这个问题,我知道我必须处理利润,但我不知道如何处理。并且使用xpd = TRUE似乎不起作用。

我的代码有这样的结构:

plot(x,y1)
par(new=TRUE)
plot(x,y2)
par(new=TRUE)
plot(x,y3)
...
par(xpd=TRUE)
legend(...)

整个代码:

time<-c(18,19, 21, 25, 26)

layer_0<-c(0.73,0.78,0.95,0.83,0.77)
layer_0_sd<-c(0.04,0.04,0.03,0.13,0.19)

layer_1<-c(0.89,0.9,0.61,0.28,0.08)
layer_1_sd<-c(0.03,0.01,0.14,0.14,0.09)

layer_2<-c(0.66,0.6,0.21,0.01,0)
layer_2_sd<-c(0.06,0,0.12,0.01,0)

layer_3<-c(0.23,0.13,0.05,0,0)
layer_3_sd<-c(0.07,0,0.03,0,0)

layer_4<-c(0.03,0.01,0.01,0,0)
layer_4_sd<-c(0.01,0,0.01,0,0)

layer_5<-c(0,0,0,0,0)
layer_5_sd<-c(0,0,0,0,0)

epsilon=0.02

plot(time, layer_0, ylim=c(0,1), type="o", lty=1, lwd=2,ylab="Longitudinal fCOVER",     xlab="Days after seeding", cex.lab=1.5, cex.axis=1.5, cex.main=1.5, cex.sub=1.5)
segments(time, layer_0-layer_0_sd,time, layer_0+layer_0_sd, lwd=2)
segments(time-epsilon,layer_0-layer_0_sd,time+epsilon,layer_0-layer_0_sd, lwd=2)
segments(time-epsilon,layer_0+layer_0_sd,time+epsilon,layer_0+layer_0_sd, lwd=2)

for (i in c(1:5)){ 
  par(new=TRUE)
  eval(parse(text=paste("plot(time, layer_",i,", ylim=c(0,1), type='o', xlab='',     ylab='', xaxt='n', yaxt='n',lwd=2, lty=",i+1,", col=",i+1,")+
  segments(time, layer_",i,"-layer_",i,"_sd,time, layer_",i,"+layer_",i,"_sd,     col=",i+1,",lwd=2)+
  segments(time-epsilon,layer_",i,"-layer_",i,"_sd,time+epsilon,layer_",i,"-layer_",i,"_sd, col=",i+1,",lwd=2)+
  segments(time-epsilon,layer_",i,"+layer_",i,"_sd,time+epsilon,layer_",i,"+layer_",i,"_sd, col=",i+1,",lwd=2)", sep="")))
}

par(xpd=TRUE)
legend(x=17.65, y=1.3, c("0-5 cm","5-10 cm", "10-15 cm", "15-20 cm", "20-25 cm","25-30 cm"), lty=c(1,2,3,4,5,6), col=c(1,2,3,4,5,6),ncol=3, lwd=2, cex=1.5)

the result

2 个答案:

答案 0 :(得分:2)

如果我必须在图表外放置一个图例,我通常会使用一个单独的面板,因为我发现控制位置和尺寸要容易得多。

layout(1:2, heights=c(1, 5))

# Legend panel
par(mar=rep(0,4))
plot(0, 0, type="n", ann=FALSE, axes=FALSE)
legend("center", c("5-10 cm", "15-20 cm", "25-30 cm"), horiz=TRUE,
       lty=2:4, col=1:3)

# Plot panel
par(mar=c(5,4,0,2))
plot(1:20, cumsum(rnorm(20)))

或者,如果您想要使用par(new=TRUE)开始使用的主题,则可以执行此操作

par(mar=c(5,4,5,2))
plot(1:20, cumsum(rnorm(20)))

par(new=TRUE, mar=c(0,0,1,0))
plot(0, 0, type="n", ann=FALSE, axes=FALSE)
legend("top", c("5-10 cm", "15-20 cm", "25-30 cm"), horiz=TRUE,
       lty=2:4, col=1:3)

两者都给出了以下结果。

enter image description here

答案 1 :(得分:2)

作为替代方案,使用参数inset可以非常轻松。可以找到类似的讨论here

为您的示例:

par(mar=c(5, 5, 6, 3), xpd=TRUE)

plot(time, layer_0, ylim=c(0,1), type="o", lty=1, lwd=2,ylab="Longitudinal fCOVER",     xlab="Days after seeding", cex.lab=1.5, cex.axis=1.5, cex.main=1.5, cex.sub=1.5)
segments(time, layer_0-layer_0_sd,time, layer_0+layer_0_sd, lwd=2)
segments(time-epsilon,layer_0-layer_0_sd,time+epsilon,layer_0-layer_0_sd, lwd=2)
segments(time-epsilon,layer_0+layer_0_sd,time+epsilon,layer_0+layer_0_sd, lwd=2)

for (i in c(1:5)){ 
  par(new=TRUE)
  eval(parse(text=paste("plot(time, layer_",i,", ylim=c(0,1), type='o', xlab='',     ylab='', xaxt='n', yaxt='n',lwd=2, lty=",i+1,", col=",i+1,")+
  segments(time, layer_",i,"-layer_",i,"_sd,time, layer_",i,"+layer_",i,"_sd,     col=",i+1,",lwd=2)+
  segments(time-epsilon,layer_",i,"-layer_",i,"_sd,time+epsilon,layer_",i,"-layer_",i,"_sd, col=",i+1,",lwd=2)+
  segments(time-epsilon,layer_",i,"+layer_",i,"_sd,time+epsilon,layer_",i,"+layer_",i,"_sd, col=",i+1,",lwd=2)", sep="")))
}

legend("top", inset = c(0, -0.25), legend=c("0-5 cm","5-10 cm", "10-15 cm", "15-20 cm", "20-25 cm","25-30 cm"), lty=c(1,2,3,4,5,6), col=c(1,2,3,4,5,6),ncol=3, lwd=2, cex=1.5)

enter image description here