Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

Changing Fonts for Graphs in R

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 text in a graph (x-label, y-label, title, legend etc.) into a uniform font e.g. Times New Roman, 12pt, Bold?

question from:https://stackoverflow.com/questions/27689222/changing-fonts-for-graphs-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the extrafont package.

install.packages("extrafont")
library(extrafont)
font_import()
loadfonts(device="win")       #Register fonts for Windows bitmap output
fonts()                       #vector of font family names
##  [1] "Andale Mono"                  "AppleMyungjo"                
##  [3] "Arial Black"                  "Arial"                       
##  [5] "Arial Narrow"                 "Arial Rounded MT Bold"  

library(ggplot2)
data(mtcars)
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 New Roman", face="bold", size=12)) #Times New Roman, 12pt, Bold
#example taken from the Github project page

enter image description here

Note: Using the extrafont package, you can also embed these fonts in PDF and EPS files (make plots in R and export to PDF/EPS). You can also directly create math symbols (see math equation in plot below), usually created using TeX. More information here and here. Also look at the github project page.

enter image description here

Also look at this answer which describes creating xkcd style graphs using the extrafont package.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...