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

2019-01-18 03:57发布

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.

标签: r plot legend
1条回答
再贱就再见
2楼-- · 2019-01-18 04:59

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

查看更多
登录 后发表回答