Change the size of the text in legend according to the length of the legend vector in the graph

我的未来我决定 提交于 2019-12-08 02:21:54

问题


I have to draw a 20 plots and horizontally place a legends in each plots.

I gave the following command for the first plot:

plot(x=1:4,y=1:4)
legend("bottom",legend = c("a","b","c","d"),horiz=TRUE,text.font=2,cex=0.64)

then for the second plot I tried :

plot(x=1:2,y=1:2)
legend("bottom",legend = c("a","b"),horiz=TRUE,text.font=2,cex=0.64)

But because the size of the character vector passed to legend argument are different I get the size of the legend different.

Since I have to plot so many different plots having varying sizes of legends,I would want to do it in an automated fashion.

Is there a way to do this which can fix the size of the legend in all the plots and fit it to graph size?


回答1:


par(cex=.64) at the beginning should suffice

op <- par(cex=.64)  # this fix the legend size for all plots
plot(x=1:4,y=1:4)
legend("bottom",legend = c("a","b","c","d"),horiz=TRUE,text.font=2) # no need to set cex anymore
plot(x=1:2,y=1:2)
legend("bottom",legend = c("a","b"),horiz=TRUE,text.font=2)
par(op) # At end of plotting, reset to previous settings


来源:https://stackoverflow.com/questions/12777976/change-the-size-of-the-text-in-legend-according-to-the-length-of-the-legend-vect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!