Change font size in legend

I have a legend in my plot, but I'm trying to increase the font size so that it matches the legendary box. When I try to increase cex as defined below. The box gets bigger and the text is still small.

code:

legend(0,16, c("Available vCPUs","Added vCPUs (1 per iteration ) "),col=c('red','black'),cex=0.39,lty=1:1,lwd=2 )

Excerpt from the chart:

enter image description here

+5
source share
3 answers

First approach:

Try setting the font size before the legend is built.

  x <- y <- rnorm(100, 0, 1) plot(x, y, type = "n") ## here you set the font size default to `x`, in this example 0.5 ## save defaults in `op` op <- par(cex = 0.5) legend("topright", legend = "foo legend", pch = 1, bty = "n") 

enter image description here

 ## here you set cexto 1.5 ## save new defaults in `op` op <- par(cex = 1.5) legend("topright", legend = "foo legend", pch = 1, bty = "n") 

enter image description here

Second approach:

Holding the pt.cex parameter equal to 1, when trying to use different values ​​for cex inside the legend call. Remember to remove the op .

 x <- rnorm(100, 10, 4) y <- rnorm(100, 10, 4) plot(x, y, type = "n") ## I tried to feed cex with 1.5 and 0.5. The font size changes while the points remain unchanged. legend("topleft", "Legend", cex=0.5, pch=1, pt.cex = 1) 

enter image description here

+8
source

You can use cex to determine the font size, use bty = 'n' to indicate the lines around the legend, then draw a rectangle separately on the graph using rect (). For instance:

 with(data, legend(-10,7, legend=c("Name_of_Legend"), bty = 'n', col=c("red"), lty=0, pch=20, cex=0.75)) with(data, rect(-10,6.2,-3,7)) 
+3
source

I think you can try using y.intersp in a legend, when the spacing between different text lines is reduced, you can increase the size of the text without changing the size of the legend window.

 legend(0,16, c("Available vCPUs","Added vCPUs (1 per iteration ) "),col=c('red','black'),cex=0.39,lty=1:1,lwd=2, y.intersp = 0.3) 
+1
source

Source: https://habr.com/ru/post/1247872/


All Articles