Plot a legend outside of the plotting area in base graphics?

前端 未结 10 921
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 14:48

As the title says: How can I plot a legend outside the plotting area when using base graphics?

I thought about fiddling around with layout

10条回答
  •  天命终不由人
    2020-11-22 15:25

    Maybe what you need is par(xpd=TRUE) to enable things to be drawn outside the plot region. So if you do the main plot with bty='L' you'll have some space on the right for a legend. Normally this would get clipped to the plot region, but do par(xpd=TRUE) and with a bit of adjustment you can get a legend as far right as it can go:

     set.seed(1) # just to get the same random numbers
     par(xpd=FALSE) # this is usually the default
    
     plot(1:3, rnorm(3), pch = 1, lty = 1, type = "o", ylim=c(-2,2), bty='L')
     # this legend gets clipped:
     legend(2.8,0,c("group A", "group B"), pch = c(1,2), lty = c(1,2))
    
     # so turn off clipping:
     par(xpd=TRUE)
     legend(2.8,-1,c("group A", "group B"), pch = c(1,2), lty = c(1,2))
    

提交回复
热议问题