ggolotの日本語表示
ggplotをPDFファイルとかに出力する時,日本語がトーフになるなどの問題があり,いつも苦労していた。
しかし,今回やってみたら普通にうまくいんですけど・・・というお話
準備
環境
- OS: Windows 10 Pro Ver. 20H2
- R4.0.5
- RStudio Version 1.4.1103
- MarkdownはTypora を利用
登録されているフォント名はデフォルトのまま
windowsFonts()
## $serif
## [1] "TT Times New Roman"
##
## $sans
## [1] "TT Arial"
##
## $mono
## [1] "TT Courier New"
特にローカルのフォントをロードしているわけでもない。
extrafont::fonts()
## NULL
・・・という環境。
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.1.0 v dplyr 1.0.5
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(ggrepel)
サンプルデータ
5点の2次元散布図
d01<-tibble(x=rnorm(5),
y=rnorm(5),
label=c("abc","123","あいう","アイウ","漢字123"))
デフォルトでやってみる
png出力
g01<-d01%>%
ggplot(aes(x=x,y=y,label=label))+
geom_point()+
geom_text_repel()+
labs(title="titleの日本語表記",
x="X軸",
y="Y軸")
g01
そのままmd変換すると,pngに出力する.
svg出力
svg出力にしたければ,Chankの記述を
```{r dev=‘svg’}
としておく。
デフォルトでsvg出力したければ,ヘッダー部分を以下のようにする。
---
title: "ggplotの日本語表示"
output:
md_document:
dev: svg
---
g01
Rの画面上では大丈夫だが,md変換してファイル出力すると,トーフになる
ggsave でファイル保存
ggsave()
関数を使うと,いろんなファイル形式で保存してくれる。
svgファイルへ
ggsave(filename="g01.svg",
plot=g01,
width=3,
height = 3,
device = svg)
日本語がトーフになる。
pdfファイルへ
ggsave(filename="g01.pdf",
plot=g01,
width=3,
height = 3,
device = cairo_pdf)
PDFも同じくトーフ。
Meiryo系フォントを指定してみる
element_text()
のところは,タイトルと軸ラベルのみ。プロットのラベルに,ライブラリ ggrepel のgeom_text_repel()
を使っているので,そこは別指定。
g02<-d01%>%
ggplot(aes(x=x,y=y,label=label))+
geom_point()+
geom_text_repel(family="Meiryo UI")+
labs(title="titleの日本語表記",
x="X軸",
y="Y軸")+
theme(text = element_text(family="Meiryo UI"))
g02
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): Windows のフォ
## ントデータベースにフォントファミリが見付かりません
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): Windows のフォ
## ントデータベースにフォントファミリが見付かりません
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): Windows のフォ
## ントデータベースにフォントファミリが見付かりません
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows のフォントデータベースにフォントファミリが見付かりません
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows のフォントデータベースにフォントファミリが見付かりません
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows のフォントデータベースにフォントファミリが見付かりません
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows のフォントデータベースにフォントファミリが見付かりません
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows のフォントデータベースにフォントファミリが見付かりません
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows のフォントデータベースにフォントファミリが見付かりません
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows のフォントデータベースにフォントファミリが見付かりません
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows のフォントデータベースにフォントファミリが見付かりません
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows のフォントデータベースにフォントファミリが見付かりません
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows のフォントデータベースにフォントファミリが見付かりません
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows のフォントデータベースにフォントファミリが見付かりません
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## Windows のフォントデータベースにフォントファミリが見付かりません
フォントファミリーが見つからないと,警告が出るが,SVG出力も
ggsave(filename="g02.svg",
plot=g02,
width=3,
height = 3,
device = svg)
PDFファイルへの出力も
ggsave(filename="g02.pdf",
plot=g02,
width=3,
height = 3,
device = cairo_pdf)
うまくいった。
警告を消す
警告がうっとうしければフォントファミリーを適当な名前で登録すればよい。
windowsFonts(
hogehoge=windowsFont(family="Meiryo UI")
)
hogehoge
という名前で登録してみた。
g02<-d01%>%
ggplot(aes(x=x,y=y,label=label))+
geom_point()+
geom_text_repel(family="hogehoge")+
labs(title="titleの日本語表記",
x="X軸",
y="Y軸")+
theme(text = element_text(family="hogehoge"))
g02
游明朝,游ゴシック,MSゴシック
Yu Mincho
でもYu Gothic
でも,MS Gothic
でもうまくいった。これらはフォントファミリーの登録もいらない?
PDFファイルへの出力も全然大丈夫。
g03<-d01%>%
ggplot(aes(x=x,y=y,label=label))+
geom_point()+
geom_text_repel(family="MS Gothic")+
labs(title="titleの日本語表記",
x="X軸",
y="Y軸")+
theme(text = element_text(family="MS Mincho"))
ggsave(filename="g03.pdf",
plot=g03,
width=3,
height = 3,
device = cairo_pdf)
g04<-d01%>%
ggplot(aes(x=x,y=y,label=label))+
geom_point()+
geom_text_repel(family="Yu Mincho")+
labs(title="titleの日本語表記",
x="X軸",
y="Y軸")+
theme(text = element_text(family="Yu Gothic"))
ggsave(filename="g04.pdf",
plot=g04,
width=3,
height = 3,
device = cairo_pdf)
たったこんだけ? どういうこと?