更改ggplot中的字体

时间:2019-09-18 09:59:49

标签: r ggplot2

我正在尝试将ggplot中的字体转换为Times New Roman。我已经加载了“额外字体”包。请参见下面的示例。我不确定为什么这行不通。


mat1 <- data.frame(matrix(nrow =10, ncol =4))
colnames(mat1) = c("Date", "Cluster 1", "Cluster 2", "Cluster 3")
date.start = as.Date("2000-01-01")
date.end = as.Date("2000-10-01")
date = seq(from = date.start, to = date.end, by = "month")
mat1[,1] = date
mat1[,2:4] = rnorm(30,0,1)





library(extrafont)
mat1 <- mat1 %>% 
  tidyr::gather(cluster, value, `Cluster 1`:`Cluster 3`)
mat1 %>% ggplot(aes(x= Date, y = value)) + 
  geom_line(aes(colour = cluster, linetype = cluster), size =1.25) +
  scale_colour_manual("",
                      values = c("Cluster 1" = "darkred", "Cluster 2" ="steelblue","Cluster 3" = "black"),
                      breaks = c("Cluster 1", "Cluster 2", "Cluster 3")) +
  scale_linetype_manual("",
                        values = c("solid", "twodash", "longdash"),
                        breaks = c("Cluster 1", "Cluster 2", "Cluster 3")) +
  xlab("Date") + ylab("Average Coefficient") + ggtitle("Market Factor") +     theme_classic() +
  theme(
    text = element_text(family = "Times"),
    plot.title = element_text(color = "black", size = 12, face = "bold", hjust = 0.5),
    axis.title = element_text(size = 12)
  )

1 个答案:

答案 0 :(得分:0)

已解决:要运行的关键点:font_import()后跟loadfonts(device="win")