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

前端 未结 10 735
佛祖请我去吃肉
佛祖请我去吃肉 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:20

    Recently I found very easy and interesting function to print legend outside of the plot area where you want.

    Make the outer margin at the right side of the plot.

    par(xpd=T, mar=par()$mar+c(0,0,0,5))
    

    Create a plot

    plot(1:3, rnorm(3), pch = 1, lty = 1, type = "o", ylim=c(-2,2))
    lines(1:3, rnorm(3), pch = 2, lty = 2, type="o")
    

    Add legend and just use locator(1) function as like below. Then you have to just click where you want after load following script.

    legend(locator(1),c("group A", "group B"), pch = c(1,2), lty = c(1,2))
    

    Try it

提交回复
热议问题