繁体   English   中英

根据图中图例向量的长度更改图例中文本的大小

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

我必须绘制20个地块,并在每个地块中水平放置一个图例。

对于第一个情节,我给出了以下命令:

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

然后我尝试了第二个情节:

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

但是因为传递给图例参数的字符向量的大小不同,所以得到的图例大小也不同。

由于必须绘制许多具有不同图例大小的不同图,因此我想以一种自动化的方式进行绘制。

有没有一种方法可以固定所有图中图例的大小并使之适合图形大小?

开头的par(cex=.64)应该足够

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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