user3580316 user3580316 - 1 month ago 7
R Question

Change font-size in legend

I have a legend in my plot, but I'm trying to increase the font size so it fit the legend-box. When I try to increase the

cex
as defined below. The box gets bigger, while 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 plot:

enter image description here

Answer Source

First approach:

Try to set the font size before to plot the legend.

 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 to 1, while trying different values for cex inside the legend call. Remember to delete 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