LoginSignup
28

More than 5 years have passed since last update.

Rでpdfに好きな日本語フォントを使用する

Last updated at Posted at 2013-09-25

Rで手っ取り早く日本語フォントを使用するにはJapan1GothicBBB等を指定しますが、
好きな日本語フォントでPDF出力したくてハマったので整理しました。

準備

R
install.packages("ggplot2")
install.packages("Cairo")

# extrafontライブラリでフォントの管理を簡略化します。
install.packages("extrafont")
require(extrafont)

# システムフォントをRのライブラリ配下にインポートします(十数分かかります。)
font_import()

# 今のセッションで使えるようにフォントをロードします。
loadfonts(quiet = TRUE)

実行

Windowsの場合

Cairoライブラリ(とpar関数?)を使います。

改めて実施してみたらpar関数無しでも日本語出力出来ました

R
require(extrafont)
require(ggplot2)
require(Cairo)

loadfonts(quiet=TRUE)

# plot作成
gg <- ggplot(mtcars,aes(x=wt,y=mpg)) + geom_point()
gg <- gg + ggtitle("車の重さと燃費の関係")
gg <- gg + xlab("重さ") + ylab("燃費")
gg <- gg + theme(text = element_text(family="Rounded M+ 2c light"))
gg <- gg + theme(plot.title = element_text(
                 face = "bold",family="Rounded M+ 2c bold"))

# PDF出力(A4横)
CairoPDF(paper="a4r",width=11.69,height=8.27)
# par(family = "Rounded M+ 2c light") # 不要?
print(gg)
dev.off()

Macの場合

Cairoライブラリを使い、CairoFontsでフォントを指定します
XQuartsのインストールが必要です。

R
require(extrafont)
require(ggplot2)
require(Cairo)

loadfonts(quiet=TRUE)

CairoFonts(regular = "Rounded M+ 2c light",bold="Rounded M+ 2c bold")

# plot作成
gg <- ggplot(mtcars,aes(x=wt,y=mpg)) + geom_point()
gg <- gg + ggtitle("車の重さと燃費の関係")
gg <- gg + xlab("重さ") + ylab("燃費")
gg <- gg + theme(plot.title = element_text(face="bold"))

# PDF出力(A4横)
CairoPDF(paper="a4r",width=11.69,height=8.27)
print(gg)
dev.off()

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28