Change the size of the text in legend according to the length of the legend vector in the graph
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
user1021713
SOreadytohelp SO has done a lot for me. Thanks to all the developers who have answered my questions. I have asked mostly questions related to R. In the year 2011 when I joined SO, i was just a beginner in R. But slowly the answers in SO and some of my own work raised my interest towards working in R. As a result, I also wanted to contribute to SO and started actively answering questions on SO and started voting as well. Since 2011, it has been wonderful experience on SO. I thank all the people for up-voting and encouraging my questions and answers and also down-voting for constructive feedback. Nowadays I am not very much active on SO. But I will take this opportunity to thank everyone again on SO for helping me.
Updated on June 04, 2022Comments
-
user1021713 almost 2 years
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?