本文へスキップ
からだにいいもの

Rのトピックスを中心に『まだ、まだ、知らない、役に立つ情報?』を発信します。

Rで解析:ggplot2の体裁に役に立つかも?「ggthemes」パッケージ

ggplot2の体裁を整えるのに役に立つかもしれないパッケージの紹介です。テーマやカラーパレット、塗りパレットなどが収録されています。

パッケージバージョンは4.2.4。R version 4.2.2で確認しています。

パッケージのインストール

下記コマンドを実行してください。

#パッケージのインストール
install.packages("ggthemes")
スポンサーリンク

コマンドの紹介

詳細はコマンド、各パッケージのヘルプを確認してください。

#パッケージの読み込み
library("ggthemes")

###データ例の作成#####
#ggplot2を利用するためにtidyverseパッケージ
#tidyverseパッケージがなければインストール
if(!require("tidyverse", quietly = TRUE)){
  install.packages("tidyverse");require("tidyverse")
}

n <- 150
TestData <- data.frame("Group" = sample(paste0("Group", 1:5), n, replace = TRUE),
                       "x" = sample(c(1:100), n, replace = TRUE),
                       "y" = sample(c(1:200), n, replace = TRUE),
                       "LETTERS" = sample(LETTERS[1:24], n, replace = TRUE))

########

#ggthemesパッケージに収録されているテーマを適用
#テーマを設定,theme_gray()はggplot2の初期値
Theme <- c("theme_gray()", "theme_base()", "theme_calc()",
           "theme_excel()", "theme_few()", "theme_fivethirtyeight()",
           "theme_gdocs()", "theme_hc()", "theme_par()",
           "theme_pander()", "theme_solarized()", "theme_stata()",
           "theme_economist()","theme_tufte()", "theme_wsj()",
           "theme_igray()", "theme_map()")
#画面分割のためgridパッケージを利用
library("grid")
#新規プロットエリア
grid.newpage()
#3行3列
pushViewport(viewport(layout = grid.layout(3, 3)))
#プロット場所指定の変数を用意
Xpos <- rep(1:3, times = 6)
Ypos <- rep(1:3, each = 3, length = 18)
#プロット
for(i in seq(Theme)){
  if(i == 10) {
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(3, 3)))}

  print(ggplot(TestData, aes(x = x, y = y)) +
          geom_point() +
          eval(parse(text = Theme[i])) +
          labs(title = Theme[i]) +
          theme(plot.title = element_text(size = 10)),
        vp = viewport(layout.pos.row = Xpos[i], layout.pos.col = Ypos[i]))}

#ggthemesパッケージに収録されているカラーテーマを適用
ColTheme <- c("scale_color_pander()", "scale_colour_canva()", "scale_colour_economist()",
              "scale_colour_few()", "scale_colour_fivethirtyeight()", "scale_colour_hc()",
              "scale_colour_ptol()", "scale_colour_stata()", "scale_colour_tableau()",
              "scale_colour_wsj()")
#画面分割のためgridパッケージを利用
library("grid")
#新規プロットエリア
grid.newpage()
#4行3列
pushViewport(viewport(layout = grid.layout(4, 3)))
#プロット場所指定の変数を用意
Xpos <- rep(1:4, times = 3)
Ypos <- rep(1:3, each = 4, length = 12)
#プロット
for(i in seq(ColTheme)){
  print(ggplot(TestData, aes(x = x, y = y, col = Group)) +
          geom_point() +
          eval(parse(text = ColTheme[i])) +
          labs(title = ColTheme[i]) +
          theme(plot.title = element_text(size = 10)),
        vp = viewport(layout.pos.row = Xpos[i], layout.pos.col = Ypos[i]))}

#ggthemesパッケージに収録されている塗テーマを適用
FillTheme <- c("scale_fill_calc()", "scale_fill_excel()",
               "scale_fill_gdocs()", "scale_fill_solarized()")
#画面分割のためgridパッケージを利用
library("grid")
#新規プロットエリア
grid.newpage()
#2行2列
pushViewport(viewport(layout = grid.layout(2, 2)))
#プロット場所指定の変数を用意
Xpos <- c(1, 2, 1, 2)
Ypos <- c(1, 1, 2, 2)
#プロット
for(i in seq(FillTheme)){
  print(ggplot(TestData, aes(x = LETTERS, fill = Group)) +
          geom_histogram(stat = "count") +
          eval(parse(text = FillTheme[i])) +
          labs(title = FillTheme[i]) +
          theme(plot.title = element_text(size = 10)),
        vp = viewport(layout.pos.row = Xpos[i], layout.pos.col = Ypos[i]))}

出力例

画像をクリックすると拡大表示します。

・収録されているテーマ

・収録されているカラーパレット

・収録されている塗りパレット

少しでも、あなたの解析が楽になりますように!!

このコードの続きをAIに聞く

スポンサーリンク
Prices and shipping availability may change. Please refer to the product page at time of purchase.
Content displayed on this site is provided by Amazon and may be updated or removed.
Amazon Associate, karada-good earns income through qualifying sales.