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

Rの解析に役に立つ記事
スポンサーリンク

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&#12497;&#12483;&#12465;&#12540;&#12472;&#12395;&#21454;&#37682;&#12373;&#12428;&#12390;&#12356;&#12427;&#12486;&#12540;&#12510;&#12434;&#36969;&#29992;
#&#12486;&#12540;&#12510;&#12434;&#35373;&#23450;,theme_gray()&#12399;ggplot2&#12398;&#21021;&#26399;&#20516;
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()")
#&#30011;&#38754;&#20998;&#21106;&#12398;&#12383;&#12417;grid&#12497;&#12483;&#12465;&#12540;&#12472;&#12434;&#21033;&#29992;
library("grid")
#&#26032;&#35215;&#12503;&#12525;&#12483;&#12488;&#12456;&#12522;&#12450;
grid.newpage()
#3&#34892;3&#21015;
pushViewport(viewport(layout = grid.layout(3, 3)))
#&#12503;&#12525;&#12483;&#12488;&#22580;&#25152;&#25351;&#23450;&#12398;&#22793;&#25968;&#12434;&#29992;&#24847;
Xpos <- rep(1:3, times = 6)
Ypos <- rep(1:3, each = 3, length = 18)
#&#12503;&#12525;&#12483;&#12488;
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&#12497;&#12483;&#12465;&#12540;&#12472;&#12395;&#21454;&#37682;&#12373;&#12428;&#12390;&#12356;&#12427;&#12459;&#12521;&#12540;&#12486;&#12540;&#12510;&#12434;&#36969;&#29992;
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()")
#&#30011;&#38754;&#20998;&#21106;&#12398;&#12383;&#12417;grid&#12497;&#12483;&#12465;&#12540;&#12472;&#12434;&#21033;&#29992;
library("grid")
#&#26032;&#35215;&#12503;&#12525;&#12483;&#12488;&#12456;&#12522;&#12450;
grid.newpage()
#4&#34892;3&#21015;
pushViewport(viewport(layout = grid.layout(4, 3)))
#&#12503;&#12525;&#12483;&#12488;&#22580;&#25152;&#25351;&#23450;&#12398;&#22793;&#25968;&#12434;&#29992;&#24847;
Xpos <- rep(1:4, times = 3)
Ypos <- rep(1:3, each = 4, length = 12)
#&#12503;&#12525;&#12483;&#12488;
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&#12497;&#12483;&#12465;&#12540;&#12472;&#12395;&#21454;&#37682;&#12373;&#12428;&#12390;&#12356;&#12427;&#22615;&#12486;&#12540;&#12510;&#12434;&#36969;&#29992;
FillTheme <- c("scale_fill_calc()", "scale_fill_excel()",
               "scale_fill_gdocs()", "scale_fill_solarized()")
#&#30011;&#38754;&#20998;&#21106;&#12398;&#12383;&#12417;grid&#12497;&#12483;&#12465;&#12540;&#12472;&#12434;&#21033;&#29992;
library("grid")
#&#26032;&#35215;&#12503;&#12525;&#12483;&#12488;&#12456;&#12522;&#12450;
grid.newpage()
#2&#34892;2&#21015;
pushViewport(viewport(layout = grid.layout(2, 2)))
#&#12503;&#12525;&#12483;&#12488;&#22580;&#25152;&#25351;&#23450;&#12398;&#22793;&#25968;&#12434;&#29992;&#24847;
Xpos <- c(1, 2, 1, 2)
Ypos <- c(1, 1, 2, 2)
#&#12503;&#12525;&#12483;&#12488;
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]))}

出力例

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

・収録されているテーマ

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

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


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

タイトルとURLをコピーしました