Last active
April 11, 2025 21:11
-
-
Save uribo/1da42a576d2f4a10a1edfb3aebaf6a4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "Quartoで日本語の作図を伴うHTMLファイルの出力" | |
format: html | |
editor: source | |
--- | |
```{r} | |
library(ggplot2) | |
library(palmerpenguins) | |
``` | |
```{r} | |
#| warning: false | |
p <- | |
ggplot(penguins) + | |
aes(x = bill_length_mm, y = bill_depth_mm) + | |
geom_point(aes(color = species, shape = species)) + | |
geom_smooth(method = "lm", aes(color = species), se = FALSE) + | |
labs( | |
title = "ペンギンのくちばしの長さと深さの関係") | |
p | |
``` | |
フォントを指定しないと「豆腐」になる(予想通り) なお、RStudioではグラフィックスデバイスのバックエンドの種類で `AGG`を指定していると文字化けしない。一方、それ以外のバックエンドの指定やPositronでは豆腐になる。 | |
フォントの指定は`theme_*()`関数の引数`base_family`で指定する。例えば、以下のように指定する。 | |
```{r} | |
#| warning: false | |
p + | |
theme_gray(base_family = "HiraKakuProN-W3") | |
``` | |
ここではmacOS標準の日本語フォントである「ヒラギノ角ゴ Pro W3」を指定するために、`HiraKakuProN-W3`を指定した。 | |
showtextパッケージを利用することで、Googleフォントの指定も可能になる。 | |
次の例は、同じく日本語フォントの「Noto Sans JP」をグラフ内で利用するものである。 | |
```{r} | |
#| eval: true | |
#| echo: true | |
library(showtext) | |
sysfonts::font_add_google("Noto Sans JP") | |
``` | |
```{r} | |
#| warning: false | |
p + | |
theme_gray(base_family = "Noto Sans JP") | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment