DJack
DJack

Reputation: 4940

Add legend at the top of a plot

My legend is cropped in my plot. To solve this, I know I have to deal with margins but I do not know how. And the use of xpd=TRUE does not seems to work.

My code has this structure:

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

The entire code:

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

Upvotes: 3

Views: 1875

Answers (2)

Marc in the box
Marc in the box

Reputation: 11995

Just as an alternative, the use of the parameter inset makes this very easy. A similar duscussion can be found here.

for your example:

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

Upvotes: 3

Backlin
Backlin

Reputation: 14842

If I have to place a legend outside a plot I generally do it with a separate panel, as I find it much easier to control positions and sizes then.

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)))

Or if you want to follow the theme you started using par(new=TRUE) you could do this

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)

Both give the result below.

enter image description here

Upvotes: 2

Related Questions