Question

I am trying to add a legend to the outside of a plot in R.

What I am using is:

png(height=400,width=842,"./rainfall.png")
par(family="serif",mar=c(4,6,4,1),oma=c(1,1,1,6),mfrow=c(1,2))

I create my plot, then:

par(xpd=TRUE)
legend(x="topright",inset=c(-0.2,0),c("4 year moving average","Simple linear trend"),lty=1,col=c("black","red"),cex=1.2)
legend("topleft",c("Annual total"),pch="*",col="blue",cex=1.2)

dev.off()

When i do this though the legend is cut off on the right, as shown in the image below. How can I get the legend to be visible outside the plot?

http://imgur.com/rpgVyrA

Just to let you know, I have been trying the suggestions in this thread, ut they are not working for me: Plot a legend outside of the plotting area in base graphics?

Any help would be appreciated, Ciara

Was it helpful?

Solution

?par, look for xpd:

A logical value or NA. If FALSE, all plotting is clipped to the plot region, if TRUE, all plotting is clipped to the figure region, and if NA, all plotting is clipped to the device region. See also clip.

Use xpd=NA so the legend is not cut off by the plot or figure region.

legend(x="topright",inset=c(-0.2,0),c("4 year moving average",
"Simple linear trend"),lty=1,col=c("black","red"),cex=1.2, xpd=NA)

Results: legend outside plot

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top