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

旧巷老猫 提交于 2019-11-26 20:39:46

问题


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.


回答1:


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:



来源:https://stackoverflow.com/questions/7136990/how-to-change-font-family-in-a-legend-in-an-r-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!