Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change font family in a legend in an R-plot?

Tags:

plot

r

legend

I have a graph use the base graphics package. For the labels on specific points I use

   text(i, MSSAcar$summary[i,7]+.7, qld$LGA[i],
   col='red',  cex=.7, family='serif')

I have also used this in the plot for main titles and axis labels. They all come out as expected.

When I add a legend I cannot seem to be able to set the font family.

Can anyone help please.

Thanks.

like image 674
John Avatar asked Aug 21 '11 07:08

John


People also ask

How do I make my legend font smaller in R?

To change the legend size of the plot, the user needs to use the cex argument of the legend function and specify its value with the user requirement, the values of cex greater than 1 will increase the legend size in the plot and the value of cex less than 1 will decrease the size of the legend in the plot.

How do I change the font size in R plot?

Go to the menu in RStudio and click on Tools and then Global Options. Select the Appearance tab on the left. Again buried in the middle of things is the font size. Change this to 14 or 16 to start with and see what it looks like.

What font is used in R plots?

You'll see the default font device "Helvetica".


1 Answers

Set the family plotting parameter before calling legend() to the value you want. Do this via an explicit call to par(). Here is a simple example

x <- y <- 1:10
plot(x, y, type = "n")
text(x = 5, y = 5, labels = "foo", family = "serif")

## set the font family to "serif"
## saving defaults in `op`
op <- par(family = "serif")

## plot legend as usual
legend("topright", legend = "foo legend", pch = 1, bty = "n")

## reset plotting parameters
par(op)

Really, you could change family before you do the first call to plot() and leave out the family = "serif" argument in the call to text(). Setting via par() is global for the current device, using parameters within function calls is local to that call.

The above code produces: use of family with legend

like image 135
Gavin Simpson Avatar answered Oct 15 '22 06:10

Gavin Simpson