12
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ggplot2を彩るカスタムテーマ15選

Last updated at Posted at 2022-02-21

0.事前準備

本記事ではRで図を作る際の「デザインテーマ」に関するパッケージ群を紹介します。

調べてみると本記事と似た記事としてthemes-to-spice-up-visualizations-with-ggplot2があったのですが、紹介されているパッケージの最終情報が古かったり、フォントの読み込みに詰まる、他にも紹介したいパッケージがある、という点から多少の内容重複を許容して記事化しようと思いました。

動作環境

今回の動作環境は

R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

loaded via a namespace (and not attached):
[1] compiler_4.1.2 tools_4.1.2  

Intel(R) Pentium(R) CPU 4415Y

RStudio-2021.09.1-372
rtools40v2-x86_64
R-4.1.2-win

にて動作を確認しました。

テーマって?

今回紹介する内容はグラフのデザインテーマと表現したほうが伝わり易いかもしれません。

ggplotにはtheme()関数が存在しています。
凡例の位置や背景の色、カラーパレット、文字フォント、軸ラベルの回転などを調整することが可能です。
しかし、theme()関数は引数が非常に多く、毎回調節していては手間と時間がかかります。

カスタムテーマとは?

カスタムテーマとは、ggplot2::theme()で細かく操作する描画設定を、一括でまとめて使えるようにした関数の事であると本記事では定義したいと思います。

カスタムテーマの関数を利用するメリットは

  • Rユーザーが複数人いる集団で統一の配色や構造を統一した図が作成できる
  • 企業や個人の特色を表現した図を作ることができる
  • 情報をオシャレに楽しく伝えられる

等ではないでしょうか。

library(ggplot2)のデフォルトで存在するカスタムテーマ

ggplot2パッケージに元からインストールされているカスタムテーマをまず紹介します。

デフォルトの設定でggplot2で図を作成すると

library(tidyverse)

p <- iris %>% 
  ggplot(aes(x=Sepal.Length,y=Sepal.Width,color=Species))+
  geom_point()+
  labs(title = "iris example",
       subtitle = "talk about themes",
       caption = "any questions? test@test.com",
       tag = "fig tag",
       x = "x label",
       y = "y label",
       colour = "legend name")

image.png

このような図になります。
ggplot2::theme_gray()をこれに付け加えてみると

p + theme_gray()

image.png

変化がありません。
実はこのtheme_gray()がデフォルトで設定されているtheme()になっています。
中をみてみると

> theme_gray
function (base_size = 11, base_family = "", base_line_size = base_size/22, 
    base_rect_size = base_size/22) 
{
    half_line <- base_size/2
    t <- theme(line = element_line(colour = "black", size = base_line_size, 
        linetype = 1, lineend = "butt"), rect = element_rect(fill = "white", 
        colour = "black", size = base_rect_size, linetype = 1), 

====長すぎるので中略=====

plot.title.position = "panel", 
        plot.subtitle = element_text(hjust = 0, vjust = 1, margin = margin(b = half_line)), 
        plot.caption = element_text(size = rel(0.8), hjust = 1, 
            vjust = 1, margin = margin(t = half_line)), plot.caption.position = "panel", 
        plot.tag = element_text(size = rel(1.2), hjust = 0.5, 
            vjust = 0.5), plot.tag.position = "topleft", 
        plot.margin = margin(half_line, half_line, half_line, 
            half_line), complete = TRUE)
    ggplot_global$theme_all_null %+replace% t
}
<bytecode: 0x000002554bcff068>
<environment: namespace:ggplot2>

このようにtheme()の細かい設定をひとまとめにした関数であることが分かります。
theme_gray()以外にもggplot2で読み込まれるカスタムテーマを試してみると

p + theme_void()

image.png

p + theme_dark()

image.png

このように大きく見た目を変更することができます。
他にもggplot2が有しているカスタムテーマには以下のようなものがあります。

theme_gray()
theme_bw()
theme_linedraw()
theme_light()
theme_dark()
theme_minimal()
theme_classic()
theme_void()

1.フォント with R

テーマを語る上で初めにフォントについてお話ししてしまいましょう。
図の中にはラベルやタイトルで文字が使われます。
文字のカタチの操作はフォントを指定することで実現できます。

フォントは

  • ダウンロードしたフォントファイルを読み込ませる
  • フォントサイトからダウンロードする関数を使い読み込ませる
    の二通りを説明します。

本記事では、パッケージ公式でリンク切れを起こしているフォントのURLについても無料の範囲でいくつか代用品を探して記載していますのでご参考になれば幸いです。

フォントをダウンロード・インストールする

windowsであればフォントファイルとして拡張子がttfやotfといったファイルを見ることが多いでしょうか。

有名なフォントサイトの「Google Fonts」から試しにフォントファイルをダウンロードしましょう。
今回は手書き感漂う英字フォントの「Architects Daughter」をサイト上の「download family」からダウンロードします。

zipを解凍後、「ArchitectsDaughter-Regular.ttf」ファイルを右クリックしてインストールを進めましょう。
インストールが完了した状態でMicrosoft PowerPointやWordのフォント一覧を見てみると使用できる状態になっているかと思います。

ライセンス契約で使うフォントの場合はインストーラーを実行するだけでこの状態になるでしょう。
※フォントにも著作権がありますので使い道によっては要確認です。
※うまく認識しない場合は「すべてのユーザーに対してインストール」を選択してください。

inst.png

Rで扱えるように読み込むライブラリ

インストールしたフォントをRで扱えるようにする必要があります。
フォント操作として有名なライブラリにextrafontやshowtextが存在しています。
今回の操作デモを用意するにあたって、私の環境で最も安定していたshowtextから紹介します。

まず、Rが認識しているフォントファミリーを調べます。

library(showtext)
font_families()
> font_families()
[1] "sans"         "serif"        "mono"         "wqy-microhei"

先ほどインストールしたフォントはファミリーに認識されていませんが、windoes内のフォントファイル置き場をfont_files()で探してみると

> font_files() %>% 
+   filter(str_detect(ps_name , "Daug")) %>% 
+   as_tibble()
# A tibble: 1 x 6
  path             file                           family     face   version   ps_name     
  <chr>            <chr>                          <chr>      <chr>  <chr>     <chr>       
1 C:/Windows/Fonts ArchitectsDaughter-Regular.ttf Architect~ Regul~ Version ~ ArchitectsD~

ちゃんとフォントファイルが追加されていることがわかります。
これをRの内部のファミリーに追加しましょう。

font_add("ArchitectsDaughter", "ArchitectsDaughter-Regular.ttf")

すると

> font_families()
[1] "sans"               "serif"              "mono"               "wqy-microhei"       "ArchitectsDaughter"

フォントファミリーとして認識されるようになりました。
追加したフォントをshowtext_auto()で使えるようにして、ggplotに当てはめてみましょう。

showtext_auto()

p+theme(
  plot.title = element_text(family = "ArchitectsDaughter", size = 18),
  plot.tag = element_text(family = "ArchitectsDaughter", size = 15),
  axis.title.x = element_text(family = "Yu Mincho", size = 11),
  axis.title.y = element_text(family = "Constantia", size = 11),
  axis.text.x = element_text(family = "sans", size = 11),
  axis.text.y = element_text(family = "sans", size = 11))

image.png

このようにフォントを調節することができました。

google fontsから直接インストール

google fontsからダウンロードしてインストールして、、、、
という手順をすっ飛ばして実行する関数がfont_add_google()です。

font_add_google("Gochi Hand", "gochi")
font_add_google("Henny Penny", "henny")
> font_families()
[1] "sans"               "serif"              "mono"               "wqy-microhei"      
[5] "Yu Mincho"          "Constantia"         "ArchitectsDaughter" "gochi"             
[9] "henny"  

上記コマンドでインストールしたフォントはこちら

https://fonts.google.com/specimen/Gochi+Hand
https://fonts.google.com/specimen/Henny+Penny

フォント読込の関数を詳しく知りたければこちら。
https://www.rdocumentation.org/packages/showtext/versions/0.9-5

2.カスタムテーマ紹介

1番目:library(vapoRwave)

Vaporwave(ヴェイパーウェイヴ)は、芸術のジャンルの一つでありwikipediaによると「過去に大量生産されて忘れ去られた人工物や技術への郷愁、消費資本主義や大衆文化、1980年代のヤッピー文化、ニューエイジへの批評や風刺として特徴づけられる」と書かれています。

そんなノスタルジックなggplotのテーマを提供してくれるのがvapoRwaveパッケージです。

テーマとして使うことが出来るのは
floralShoppe()
newRetro()
jwz()
です。

それぞれテーマを作るにあたって元ネタがあるので、ご興味があれば調べてみてください。
使われるフォントもパッケージ作者のgithubからダウンロードできます。
https://github.com/moldach/vapoRwave/tree/master/inst

devtools::install_github("moldach/vapoRwave")
library(vapoRwave)

font_add("VCR OSD Mono", "VCR_OSD_MONO_1.001.ttf")
showtext_auto()

p + labs(title="Floral Shoppe", 
       subtitle="iris exsample",
       tag="fig tag") + 
  floral_shoppe()
  # カラーパレット指定する関数もある 例:scale_fill_floralShoppe()

image.png

font_add("SF Alien Encounters", "SFAlienEncounters.ttf")
showtext_auto()

p+labs(title="New Retro Theme",
     subtitle="iris exsample",
     caption = "caption") + 
  new_retro() 

image.png

font_add("Windows Command Prompt", "windows_command_prompt.ttf")
showtext_auto()

p + labs(title="JWZ Style", 
     subtitle="iris exsample",
     caption="caption") + 
  jwz()

image.png

配布フォントがうまく機能しない場合もあるので代替品を探しておきました。

Alien Encounters - FREE 80s Font Family - Hipsthetic
http://www.hipsthetic.com/alien-encounters-free-80s-font-family/
  
VCR OSD Mono - Free Pixel Font - Hipsthetic
http://www.hipsthetic.com/vcr-osd-mono-free-pixel-font/
  
Windows Command Prompt Font | dafont.com
https://www.dafont.com/windows-command-prompt.font

Blade Runner Movie Font | Phil Steinschneider | FontSpace
https://www.fontspace.com/blade-runner-movie-font-f3516

Streamster - FREE 80s Script Font - Hipsthetic
http://www.hipsthetic.com/streamster-free-80s-script-font/

2番目:library(ggtech)

airbnbやgoogle、facebook等の有名な企業のフォントや色スタイルを模倣したテーマを提供しています。
日本ではあまりなじみのない企業もありますが、それぞれの企業特色を出したplotテーマとしてよくできています。

devtools::install_github("ricardo-bion/ggtech",dependencies=TRUE,force=T)
library(ggtech)

font_add("Circular Air Medium", "Circular Air-Medium 3.46.45 PM.ttf")
font_add("Circular Air Bold", "Circular Air-Bold 3.46.45 PM.ttf")
font_add("Circular Air Light", "Circular Air-Light 3.46.45 PM.ttf")
showtext_auto() 

p + theme_tech(theme="airbnb") + 
  labs(title="Airbnb theme", 
       subtitle="iris exsample")

image.png

p + theme_airbnb_fancy() + 
  labs(title="Airbnb theme", 
       subtitle="iris exsample")

image.png

font_add("Georgia", "georgia.ttf")
showtext_auto() 

p + theme_tech(theme="etsy") + 
  labs(title="Etsy theme", 
       subtitle="iris exsample")

image.png

font_add("Facebook Letter Faces", "FACEBOLF.OTF")
showtext_auto() 

p + theme_tech(theme="facebook") +
  labs(title="Facebook theme", 
       subtitle="iris exsample")

image.png

font_add("Product Sans", "product-sans.ttf")
showtext_auto() 

p + theme_tech(theme="google") + 
  labs(title="Google theme", 
       subtitle="iris exsample")

image.png

font_add("[z] Arista Light", "arista-light.ttf")
font_add("PicoBlackAl", "pico-black.ttf")
showtext_auto() 

p + theme_tech(theme="twitter") + 
  labs(title="Twitter theme", 
       subtitle="iris exsample")

image.png

font_add("Helvetica", "Helvetica.ttf")
font_add("Helvetica Neue Bold", "Helvetica Neue LT Pro 75 Bold.otf")
showtext_auto() 

p + theme_tech(theme="X23andme") + 
  labs(title="X23andme theme", 
       subtitle="iris exsample")

image.png

air bnb font 
https://github.com/ricardo-bion/ggtech

Helvetica - Helvetica Neue BoldはなかったのでHelvetica Neue LT Pro 75 Boldで代用
https://www.fontyukle.net/search/helvetica
https://www.fontyukle.net/font/Helvetica-Neue-LT-Pro-75-Bold

Etsy
https://www.etsy.com/assets/type/Guardian-EgypTT-Text-Regular.ttf

Twitter
http://social-fonts.com/assets/fonts/arista-light/arista-light.ttf
http://social-fonts.com/assets/fonts/pico-black/pico-black.ttf

Google
http://social-fonts.com/assets/fonts/product-sans/product-sans.ttf

face book
https://fonts2u.com/facebookletterfaces.font
予備http://social-fonts.com/assets/fonts/facebook-letter-faces/facebook-letter-faces.ttf

補足説明:
ggplotの各点を企業マークにする関数も内包しています。
ただ、X23andme、Etsyは元リンクからのアクセスが切れている様子。

library(RCurl)
data <- data.frame(x = c(1:4, 3:1), y=1:7)

ggplot(aes(x,y), data=data) + 
  geom_tech(size=1, theme="airbnb") + 
  theme_tech("airbnb") +
  ggtitle("Airbnb geom")

ggplot(aes(x,y), data=data) + 
  geom_tech(size=1, theme="facebook") + 
  theme_tech("facebook")+
  ggtitle("Facebook geom")

image.png

image.png

3番目:library(tvthemes)

テレビ番組をもとに作成したggplot2テーマです。
ブルックリン・ナイン-ナインや、シンプソンズ、スポンジボブ、等のフォントと配色が楽しめます。
それぞれの関数の元ネタはご興味に応じて調べてみてください。

devtools::install_github("Ryo-N7/tvthemes")
library(tvthemes)

font_add("Calibri Light", "calibril.ttf")
font_add("Titillium Web", "TitilliumWeb-Regular.ttf")
showtext_auto() 

p +
labs(title = "Brooklyn Nine-Nine",
     subtitle = "iris exsample",
     caption = "caption") +
theme_brooklyn99(title.font = "Titillium Web",
                 text.font = "Calibri Light",
                 subtitle.size = 14)

image.png

font_add("Some Time Later", "Some-Time-Later.ttf")
showtext_auto()

p +
labs(title = "spongeBob",
     subtitle = "iris exsample",
     caption = "caption") +
  theme_spongeBob(title.font = "Some Time Later",
                  text.font = "Some Time Later",
                  title.size = 22,
                  subtitle.size = 16,
                  axis.title.size = 16,
                  axis.text.size = 14,
                  legend.title.size = 14)

image.png

font_add("Akbar", "akbar.ttf")
showtext_auto()

p +
labs(title = "The Simpsons",
     subtitle = "iris exsample",
     caption = "caption") +
  theme_simpsons(title.font = "Akbar",
                 text.font = "Akbar",
                 axis.text.size = 8)

image.png

font_add("Get Schwifty", "get_schwifty.ttf")
showtext_auto()

p +
labs(title = "Rick and Morty",
     subtitle = "iris exsample",
     caption = "caption") +
  theme_rickAndMorty(title.font = "Get Schwifty",
                     text.font = "Get Schwifty",
                     title.size = 14)

image.png

font_add("Slayer", "slayer11.TTF")
showtext_auto()

p +
labs(title = "Avatar",
     subtitle = "iris exsample") +
  theme_avatar(title.font = "Slayer",
                         text.font = "Slayer",
                         title.size = 10)

image.png

font_add("Titillium Web", "TitilliumWeb-Regular.ttf")
font_add("Titillium Web Black", "TitilliumWeb-Black.ttf")
showtext_auto()

p +
labs(title = "Parks and Recreation", 
     subtitle = "iris exsample",
     caption = "caption") + 
  scale_color_parksAndRec() + 
  theme_minimal() +
  theme_parksAndRec(text.font = "Titillium Web",
                    title.font = "Titillium Web Black",
                    legend.font = "Titillium Web")

image.png

font_add("Chelsea Market", "ChelseaMarket-Regular.ttf")
showtext_auto()

p +
labs(title = "hildaNight",
     subtitle = "iris exsample",
     caption = "caption")+
  theme_hildaNight()

p+
  labs(title = "hildaDusk",
       subtitle = "iris exsample",
       caption = "caption")+
  theme_hildaDusk()

p+
  labs(title = "hildaDay",
       subtitle = "iris exsample",
       caption = "caption")+
  theme_hildaDay()

image.png

image.png

image.png

https://github.com/Ryo-N7/tvthemes/tree/master/inst/fonts

Calibri Light - 標準装備だと思うけど一応
https://fontsdata.com/141929/calibrilight.htm

4番目:library(hrbrthemes)

hrbrmstrの提供する他のパッケージ(hrbrmisc)からplotテーマだけを抜き出したものです。
開発者本人は、あくまでも個人的につくっている関数なのでCRANに追加する予定はないとのこと。

使える関数は

theme_ipsum
theme_ipsum_gs
theme_ipsum_es
theme_ipsum_rc
theme_ipsum_ps
theme_ipsum_pub
theme_ipsum_tw 
theme_modern_rc
theme_ft_rc

関数の末尾についているいくつかのアルファベットは使われるフォントを示しています。
たとえば
ps = IBM Plex Sansフォント
tw = Titilium Web
のような関係になっています。

remotes::install_gitlab("hrbrmstr/hrbrthemes")
library(hrbrthemes)

font_add("Arial Narrow", "ARIALN.TTF")
showtext_auto()

p +
labs(title="theme_ipsum",
     subtitle="iris exsample",
     caption="caption") + 
  theme_ipsum()

image.png

p +
  labs(title="theme_ipsum_rc",
       subtitle="iris exsample",
       caption="caption") + 
  theme_ipsum_rc()

image.png

p +
  labs(title="theme_ft_rc",
       subtitle="iris exsample",
       caption="caption") + 
  theme_ft_rc()

image.png

font_add("IBMPlexSans", "IBMPlexSans-Medium.ttf")
font_add("IBMPlexSans-Bold", "IBMPlexSans-Bold.ttf")
showtext_auto()

p +
  labs(title="theme_ipsum_ps",
       subtitle="iris exsample",
       caption="caption") +
  theme_ipsum_ps()

image.png

p +
  labs(title="theme_ipsum_tw",
       subtitle="iris exsample",
       caption="caption") +
  theme_ipsum_tw()

image.png

p +
  labs(title="theme_ipsum_rc",
       subtitle="iris exsample",
       caption="caption") +
  theme_ipsum_rc()

image.png

Arial Narrow
https://freefontsdownload.net/free-arial-narrow-font-30388.htm

IBMPlexSans-Medium
https://github.com/IBM/plex/tree/master/IBM-Plex-Sans/fonts/complete/ttf

5番目:library(ggpomological)

米国農務省水彩画コレクションをもとにしたカスタムテーマです。
水彩画の紙の雰囲気を表現した背景、手書きフォント(Homemade Apple) を使ったplotが楽しめます。

デフォルトでextrafontsをつかうのでインストールしておきましょう。
windowsはRttf2pt1を使わないと「フォント名が見つからない」エラーになるのでこれも合わせてインストールしておきましょう

devtools::install_github("gadenbuie/ggpomological")
library(ggpomological)

remotes::install_version("Rttf2pt1", version = "1.3.8")
extrafont::font_import()
extrafont::loadfonts(device = "win")
extrafont::fonts()

p + theme_pomological()
p + theme_pomological_fancy()

image.png

image.png

Mr De Haviland - Google Fonts
https://fonts.google.com/specimen/Mr+De+Haviland

Homemade Apple - Google Fonts
https://fonts.google.com/specimen/Homemade+Apple

Marck Script - Google Fonts
https://fonts.google.com/specimen/Marck+Script

Mr Bedfort - Google Fonts
https://fonts.google.com/specimen/Mr+Bedfort

6番目:library(xaringanthemer)

Rmdでスライドを作ったことがある方ならば一度は使われたことがあるかもしれません。
無機質なRmdスライドをポップな色使いにしてくれるxaringanパッケージの「色テーマ」を中心に抜き出したパッケージです。
スライド内だけでなく、ggplotのテーマとして使うことができます。

devtools::install_github("yihui/xaringan",force = TRUE)
remotes::install_github("gadenbuie/xaringanthemer",force = TRUE)

library(xaringan)
library(xaringanthemer)

style_duo(text_font_size = "1rem",base_font_size = "10px")
p + 
  theme_xaringan()

image.png

style_mono_accent(text_font_size = "1rem",base_font_size = "10px")
p + 
  theme_xaringan()

image.png

style_mono_dark(text_font_size = "1rem",base_font_size = "10px")
p + 
  theme_xaringan()

image.png

style_solarized_dark(text_font_size = "1rem",base_font_size = "10px")
p + 
  theme_xaringan()

image.png

style_xaringan(text_font_size = "1rem",base_font_size = "10px")
p + 
  theme_xaringan(text_font = google_font("Ranga"),
                 title_font = google_font("Reenie Beanie"))

上記のいくつかの例からお分かりかもしれませんが、まずは使用するスタイルをstyle_*()関数で指定します。
でないとスタイルcssが見つからないと怒られます。

使えるスタイルは

style_duo()
style_duo_accent()
style_duo_accent_inverse()
style_mono_accent()
style_mono_accent_inverse()
style_mono_dark()
style_mono_light()
style_solarized_dark()
style_solarized_light()
style_xaringan()

自分で作ったcssを使う関数もありますので、よりカスタムしたい場合にはそちらから。

6番目:library(bbplot)

パッケージ名にもなっているBBCのBBC Data and Visualisation Journalismチームが開発したカスタムテーマです。
説明によると
「bbc独自のスタイルで出版に適したグラフィックを作成するプロセスをより再現性の高いものにし、また、Rを初めて使う人でも簡単にグラフィックを作成できるように、RパッケージとRクックブックを開発しました。」
とのこと

devtools::install_github('bbc/bbplot')
library(bbplot)

font_add("Helvetica", "Helvetica.ttf")
showtext_auto() 

p +
  bbc_style()

image.png

7番目:library(artyfarty)

artyfartyはAIやデータ戦略の設計を行っているdataroots社の提供するカラーテーマパッケージです。

使用できるテーマは

theme_bain
theme_dataroots
theme_economist
theme_empty
theme_farty
theme_five38
theme_flat
theme_ft
theme_monokai_full
theme_monokai
theme_retro
theme_scientific

です。

devtools::install_github('datarootsio/artyfarty')
library(artyfarty)

p +
  theme_dataroots() +
  ggtitle("theme_dataroots")

image.png

p +
  theme_monokai() +
  ggtitle("theme_monokai")

image.png

8番目:library(ggthemes)

ggplotをexcelやウォール・ストリート・ジャーナル、エコノミスト、ファイブサーティエイト、Highcharts等の有名な雑誌やwebサイトのカラーテーマを取り扱うパッケージです。
他にも複数のカラーテーマを取り扱っています。

theme_base()
theme_calc()
theme_clean()
theme_economist() 
theme_economist_white()
theme_excel()
theme_excel_new()
theme_few()
theme_fivethirtyeight()
theme_foundation()
theme_gdocs()
theme_hc()
theme_igray()
theme_map()
theme_pander()
theme_par()
theme_solarized() 
theme_solarized_2()
theme_solid()
theme_stata()
theme_tufte()
theme_wsj()
install.packages('ggthemes', dependencies = TRUE)
library(ggthemes)
p+
  theme_economist() 

image.png

p+
  theme_hc()

image.png

p+
  theme_wsj()

image.png

9番目:library(gghighcontrast)

図を描く際にコントラストを強調するために制作されたパッケージです。

devtools::install_github("topfunky/gghighcontrast")
library(gghighcontrast)

font_add("InputMono", "InputMono-Medium.ttf")
showtext_auto()

p+
theme_high_contrast(
  foreground_color = "white",
  background_color = "black",
  base_family = "InputMono"
)

image.png

p+
  theme_high_contrast(
    foreground_color = "#00ff00",
    background_color = "black",
    base_family = "InputMono"
  )

image.png

InputMono
https://input.djr.com/download/

10番目:library(ggdark)

暗いテーマを基調としたテーマ群を提供しているパッケージです。

dark_theme_minimal()
dark_theme_dark()
dark_theme_gray()
dark_theme_bw()
dark_theme_light()
dark_theme_linedraw()
dark_theme_classic()
dark_theme_void()
devtools::install_github("nsgrantham/ggdark")
library(ggdark)

p + dark_theme_void()

image.png

11番目:library(xkcd)

有名なコミックをテーマとしたフォントと描画設定を含んでいるパッケージです。

#remotes::install_version("Rttf2pt1", version = "1.3.8")
install.packages("xkcd")

library(xkcd)
library(extrafont)

font_import(pattern = "[X/x]kcd", prompt=FALSE)

p +
  theme_xkcd()

image.png

フォントだけでなく、軸や図の線をコミックのようにゆがませる関数も含まれています。
ただし、これを使うときはggplot()の中は空でないといけません。

iris %>% 
  ggplot()+
  geom_point(aes(x=Sepal.Length,y=Sepal.Width,color=Species))+
  labs(title = "iris example",
       subtitle = "talk about themes",
       caption = "any questions? test@test.com",
       tag = "fig tag",
       x = "x label",
       y = "y label",
       colour = "legend name")+
  theme_xkcd() +
  xkcdaxis(range(iris$Sepal.Length),range(iris$Sepal.Width))

image.png

xkcd
http://simonsoftware.se/other/xkcd.ttf

12番目:ggplot_theme_Publication-2.R

こちらはパケージではありませんがkoundyさんの定義した関数ggplot_theme_Publicationの中に含まれている二つのテーマ設定関数を紹介します。

theme_dark_blue()
theme_dark_grey()
download.file("https://raw.githubusercontent.com/koundy/ggplot_theme_Publication/master/ggplot_theme_Publication-2.R", destfile = "ggplot_theme_Publication-2.R",method = "curl")
source('ggplot_theme_Publication-2.R')

p + 
  theme_dark_blue()

image.png

13番目:library(thematic)

図、shiny、マークダウンの中の配色やスタイルのグローバル設定を変更するパッケージです。
テーマを指定しながらon,off関数で操作します。

install.packages("thematic")
library(thematic)

thematic_on(bg = "#222222", fg = "white", accent = "#0CE3AC", font = "Oxanium")
p
thematic_off()

image.png

14番目:library(ggthemr)

thematicではon,off関数による制御でしたが、ggthemrではすでにテーマごとに関数化して言います。
使うことのできるテーマは以下です。

flat
flat dark
camouflage
chalk
copper
dust
earth
fresh
grape
grass
greyscale
light
lilac
pale
sea
sky
solarized
devtools::install_github('Mikata-Project/ggthemr')
library(ggthemr)

ggthemr('dust')
p
ggthemr_reset()

image.png

ggthemr('copper')
p
ggthemr_reset()

image.png

余白まで配色テーマを使いたい場合はtype引数で操作します。

ggthemr('copper', type = 'outer')
p
ggthemr_reset()

image.png

15番目:library(withr)

ggplotとは外れてしまいますが、グローバル設定を限られた範囲で適応するパッケージです。
グローバル設定に影響を与えてしまう引数やパッケージの読み込みを限定的に実行し、実行後にリセットしてくれます。

par("col" = "black")
my_plot <- function(new) {
  with_par(list(col = "red", pch = 19),
    plot(mtcars$hp, mtcars$wt)
  )
  par("col")
}
my_plot()

README-unnamed-chunk-3-1.png

3.以上

15番目はggplot限定の話から外れてしまいましたが、ある程度のggplotの配色やフォントを操作するカスタムテーマを持ったパッケージ達をご紹介できたかと思います。

12
14
0

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
12
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?