简体   繁体   中英

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:

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?

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 :

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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