简体   繁体   English

使用R中的表达式功能更改图例中的文本字体

[英]Change text font in legend using expression function in R

I have the following legend which I am trying to change to the font Times New Roman: 我尝试将以下图例更改为Times New Roman字体:

legend(x = 23, y = 40, legend = c(expression(bold('Dawn Col-0 Control')), expression(bold('Dusk Col-0 Control')), expression(bold('Dawn Col-0 100g ha'^'-1')), expression(bold('Dusk Col-0 100g ha'^'-1'))), col = c('black', 'red','black', 'red'), lty = c(1,1,2, 2), pch=c(19,19,19,19), cex = 1.5, bty="n", lwd=2)

Because I am using the expression function, 因为我正在使用表达式函数,

par(family="A", font=2) 

doesn't work when this is called before or after the legend. 在图例之前或之后调用它时不起作用。 Any ideas how I can change the font to Times New Roman? 有什么想法可以将字体更改为Times New Roman吗?

I suppose it will depend on your machine. 我想这将取决于您的计算机。 But for me, on my windows, i can do these to get the legend with times new roman font even when using the expression in legend : 但是对我来说,即使在legend使用expression ,我也可以在Windows上执行以下操作以使用times new roman字体获得legend

par(family = "serif")
plot(1:30, 21:50)
legend(x = 15, y = 40,
       legend = c(expression(bold('Dawn Col-0 Control')), 
                  expression(bold('Dusk Col-0 Control')), 
                  expression(bold('Dawn Col-0 100g ha'^'-1')),
                  expression(bold('Dusk Col-0 100g ha'^'-1'))),
       col = c('black', 'red','black', 'red'), lty = c(1,1,2, 2), pch=c(19,19,19,19), cex = 1.5, bty="n", lwd=2)

If you run windowsFonts() command, it will tell you which fonts are available to you by default (for me, "serif" refers to the Times New Roman font and "sans" to the Arial) 如果您运行windowsFonts()命令,它将默认告诉您哪些字体可用(对我而言,“ serif”是指Times New Roman字体,“ sans”是Arial)。

Now if times new roman is not in your list of fonts, you can add it by: 现在,如果新字体不在您的字体列表中,则可以通过以下方式添加它:

windowsFonts(TimesNewRoman  = windowsFont("Times New Roman"))

And then create the plot as: 然后将图创建为:

par(family = "TimesNewRoman")
plot(1:30, 21:50)
legend(x = 15, y = 40,
       legend = c(expression(bold('Dawn Col-0 Control')), 
                  expression(bold('Dusk Col-0 Control')), 
                  expression(bold('Dawn Col-0 100g ha'^'-1')),
                  expression(bold('Dusk Col-0 100g ha'^'-1'))),
       col = c('black', 'red','black', 'red'), lty = c(1,1,2, 2), pch=c(19,19,19,19), cex = 1.5, bty="n", lwd=2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM