Changing Fonts for Graphs in R

前端 未结 4 633
感情败类
感情败类 2020-11-28 06:22

In my study I am generating various graphs using R. I see that most of the graphs come up with a Sans Serif type font with various sizes.

How to I change all the te

4条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 06:45

    Here's a ggplot solution using WindowsFonts(...)

    windowsFonts(Times=windowsFont("Times New Roman"))
    library(ggplot2)
    ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
      ggtitle("Fuel Efficiency of 32 Cars") +
      xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
      theme_bw() +
      theme(text=element_text(family="Times", face="bold", size=12)) #Times New Roman, 12pt, Bold
    

    As you can see, the text really is Times New Roman.

    The main idea is that whatever name you give the font internally in R, using

    windowsFonts(name=windowsFont("system name"))
    

    you should use to reference the font in

    theme(text=element_text(family="name",...),...)
    

提交回复
热议问题