指定繪圖字型

功能說明

    • 令 R 可使用本機所有可用字型

    • 範例分一般版 (plot) 及進階版 (ggplot2)

    • 範例輸出至 PDF 格式

    • 在進階版範例中,使用數學式當標籤

事前準備:當標籤

    • install.packages("extrafont")

    • library(extrafont)

    • font_import()

程式碼

library(extrafont) loadfonts() pdf("1.pdf", family="Ubuntu Mono", width=4, height=4) plot(mtcars$mpg, mtcars$wt, main = "Fuel Efficiency of 32 Cars", xlab = "Weight (x1000 lb)", ylab = "Gallon/Miles") dev.off()

安裝後檢查

    • fonts()

運用字型

    • library(extrafont)

    • loadfonts()

程式碼

library(ggplot2) library(extrafont) loadfonts() p <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() + ggtitle("Fuel Efficiency of 32 Cars") + ylab("Gallon/Miles") + theme_bw() + theme(text=element_text(family="Ubuntu Mono", size=12)) + xlab(expression(Weight:~{10}^{3}~lb)) ggsave("2.pdf", p, width=4, height=4)