简体   繁体   中英

Adding a legend to the outside of a multiple graph plot in R

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

?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: 情节外的传说

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM