Rで解析:「ggplot2」の体裁を色々整える「ggh4x」パッケージ

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

「ggplot2」パッケージの「facet_wrap」や「facet_grid」コマンドを使いやすくするだけでなく、軸や凡例の体裁を色々整えることが可能なパッケージの紹介です。「ggplot2」パッケージ単独でも体裁を整えることが可能ですが、本パッケージを利用することで大幅な作業時間を短縮できると考えます。

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

スポンサーリンク

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

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

#パッケージのインストール
install.packages("ggh4x")

実行コマンドの紹介

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

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

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

set.seed(1234)
n <- 50
TestData <- tibble(Group = sample(paste0("Group ", 1:2), n, replace = TRUE),
                   SubGroup = sample(paste0("SubGroup ", 1:3), n, replace = TRUE),
                   Data_X = 1:n,
                   Data_Y = rnorm(n))
########

###&#22522;&#28310;&#12503;&#12525;&#12483;&#12488;&#12398;&#20316;&#25104;#####
TestPlot <- ggplot(TestData, aes(x = Data_X, y = Data_Y, color = Group)) +
  geom_point() +
  theme(axis.line = element_line(colour = "black"))
#####

#ggplot::facet_wrap&#12398;&#25313;&#24373;:facet_wrap2&#12467;&#12510;&#12531;&#12489;
#facets&#12398;&#26465;&#20214;&#12434;&#25351;&#23450;:facets&#12458;&#12503;&#12471;&#12519;&#12531;;vars&#12467;&#12510;&#12531;&#12489;&#12434;&#20351;&#29992;&#12375;&#12390;&#35373;&#23450;
#&#20006;&#12409;&#12427;&#34892;&#25968;,&#21015;&#25968;&#12434;&#35373;&#23450;:nrow,ncol&#12458;&#12503;&#12471;&#12519;&#12531;
#&#36600;&#12398;&#22266;&#23450;&#26041;&#27861;:scales&#12458;&#12503;&#12471;&#12519;&#12531;;"fixed","free_x","free_y","free"
#&#36600;&#12398;&#34920;&#31034;&#35373;&#23450;:axes&#12458;&#12503;&#12471;&#12519;&#12531;;"margins","x","y","all"
#&#36600;&#12521;&#12505;&#12523;&#12398;&#34920;&#31034;&#35373;&#23450;:remove_labels&#12458;&#12503;&#12471;&#12519;&#12531;;"none","x","y","all"
#axes&#12458;&#12503;&#12471;&#12519;&#12531;&#12398;&#36600;&#12395;&#36969;&#24540;&#12373;&#12428;&#12414;&#12377;
#&#12503;&#12525;&#12483;&#12488;&#26041;&#21521;:dir&#12458;&#12503;&#12471;&#12519;&#12531;;"h","v"
TestPlot +
  facet_wrap2(facets = vars(Group), nrow = NULL, ncol = NULL,
              scales = "free", axes = "margins",
              remove_labels = "none",
              shrink = TRUE, dir = "v")

#ggplot::facet_grid&#12398;&#25313;&#24373;:facet_grid2&#12467;&#12510;&#12531;&#12489;
#&#12458;&#12503;&#12471;&#12519;&#12531;&#12398;&#22810;&#12367;&#12399;facet_wrap2&#12467;&#12510;&#12531;&#12489;&#12392;&#20849;&#36890;
#&#21015;&#26041;&#21521;&#12398;&#20998;&#21106;&#25351;&#27161;:rows&#12458;&#12503;&#12471;&#12519;&#12531;;vars&#12467;&#12510;&#12531;&#12489;&#12434;&#20351;&#29992;&#12375;&#12390;&#35373;&#23450;
#&#34892;&#26041;&#21521;&#12398;&#20998;&#21106;&#25351;&#27161;:cols&#12458;&#12503;&#12471;&#12519;&#12531;;vars&#12467;&#12510;&#12531;&#12489;&#12434;&#20351;&#29992;&#12375;&#12390;&#35373;&#23450;
TestPlot +
  facet_grid2(rows = vars(Group), cols = vars(SubGroup),
              scales = "fixed", space = "fixed",
              axes = "margins", remove_labels = "none")

#&#20837;&#12428;&#23376;&#12398;facet&#12503;&#12525;&#12483;&#12488;:facet_nested&#12467;&#12510;&#12531;&#12489;
#&#12521;&#12505;&#12523;&#12398;&#20301;&#32622;&#12434;&#25351;&#23450;:switch&#12458;&#12503;&#12471;&#12519;&#12531;;"x","y","both"
TestPlot +
  facet_nested(~ Group + SubGroup, 
               nest_line = element_line(linetype = 2,
                                        colour = "blue"),
               switch = "y")

#&#12524;&#12452;&#12450;&#12454;&#12488;&#12434;&#25351;&#23450;&#12375;&#12390;&#12503;&#12525;&#12483;&#12488;:facet_manual&#12467;&#12510;&#12531;&#12489;
#&#12524;&#12452;&#12450;&#12454;&#12488;&#12434;matrix&#12391;&#25351;&#23450;&#12377;&#12427;
design <- matrix(c(1,2,1,2), 2, 2)
#&#12503;&#12525;&#12483;&#12488;
TestPlot +
  facet_manual(vars(Group), design = design)

#&#12371;&#12435;&#12394;&#20351;&#12356;&#26041;&#12418;&#20986;&#26469;&#12414;&#12377;
#&#12524;&#12452;&#12450;&#12454;&#12488;&#12434;&#25991;&#23383;&#12392;#(&#31354;&#30333;)&#12391;&#25351;&#23450;&#12377;&#12427;
#&#25913;&#34892;&#12391;&#34892;&#12434;&#20999;&#12426;&#26367;&#12360;&#12427;
design <- "
A#
#B
"
#&#12503;&#12525;&#12483;&#12488;
TestPlot +
  facet_manual(vars(Group), design = design,
               heights = unit(c(4, 3), "cm"),
               widths = unit(c(6, 3), "cm"))

#&#21508;&#12467;&#12510;&#12531;&#12489;&#12398;strip&#12458;&#12503;&#12471;&#12519;&#12531;&#12395;&#20307;&#35009;&#12434;&#36969;&#24540;&#12377;&#12427;:strip_themed&#12467;&#12510;&#12531;&#12489;
TestStrip <- strip_themed(
  #&#27700;&#24179;&#26041;&#21521;&#12398;&#20307;&#35009;
  #list&#12391;&#21508;&#12521;&#12505;&#12523;&#12398;&#33394;&#12434;&#35373;&#23450;&#21487;&#33021;:fill,color
  background_x = 
    list(element_rect(fill = "dodgerblue"),
         element_rect(fill = "green"),
         element_rect(fill = "white")),
  text_x = elem_list_text(colour = "black",
                          face = "bold"),
  #by_layer_x&#12458;&#12503;&#12471;&#12519;&#12531;:FALSE:&#30064;&#12394;&#12427;&#35201;&#32032;&#12434;&#20491;&#12293;&#12395;&#36969;&#24540;
  by_layer_x = FALSE,
  #&#22402;&#30452;&#26041;&#21521;&#12398;&#20307;&#35009;
  background_y = elem_list_rect(
    fill = c("gold", "red")
  ),
  text_y = elem_list_text(angle = c(0, 45)),
  #by_layer_y&#12458;&#12503;&#12471;&#12519;&#12531;:FALSE:&#30064;&#12394;&#12427;&#35201;&#32032;&#12434;&#20491;&#12293;&#12395;&#36969;&#24540;
  by_layer_y = TRUE
)

#&#12503;&#12525;&#12483;&#12488;
TestPlot +
  facet_grid2(rows = vars(Group), cols = vars(SubGroup),
              scales = "fixed", space = "fixed",
              axes = "margins", remove_labels = "none",
              strip = TestStrip)

#facet_grid&#12398;&#21508;&#12497;&#12493;&#12523;&#12469;&#12452;&#12474;&#12434;&#35519;&#25972;:force_panelsizes&#12467;&#12510;&#12531;&#12489;
#&#21015;&#26041;&#21521;&#12398;&#12469;&#12452;&#12474;:cols&#12458;&#12503;&#12471;&#12519;&#12531;
#&#34892;&#26041;&#21521;&#12398;&#12469;&#12452;&#12474;:rows&#12458;&#12503;&#12471;&#12519;&#12531;
#&#34892;&#21015;&#12398;&#27604;&#29575;&#12434;&#36899;&#21205;:respect&#12458;&#12503;&#12471;&#12519;&#12531;;TRUE/FALSE
TestPlot +
  facet_grid(Group ~ SubGroup) +
  force_panelsizes(cols = c(1, 0.5, 0.8), 
                   rows = c(1, 3),
                   respect = FALSE)

###facet_grid&#24460;&#12398;&#21508;&#12464;&#12523;&#12540;&#12503;&#12408;&#33394;&#12434;&#35373;&#23450;&#12377;&#12427;:scale_colour_multi&#12467;&#12510;&#12531;&#12489;#####
#&#12487;&#12540;&#12479;&#12398;&#20316;&#25104;:SubGroup&#27598;&#12395;&#12487;&#12540;&#12479;&#12434;&#36969;&#24540;&#12377;&#12427;
TestPlot <- ggplot(TestData, aes(x = Data_X, y = Data_Y, color = Group)) +
  geom_point(aes(SG1 = Data_X),
             data = ~ subset(., SubGroup == "SubGroup 1")) +
  geom_point(aes(SG2 = Data_X),
             data = ~ subset(., SubGroup == "SubGroup 2")) +
  geom_point(aes(SG3 = Data_X),
             data = ~ subset(., SubGroup == "SubGroup 3"))

#scale_colour_multi&#12467;&#12510;&#12531;&#12489;
#&#36969;&#24540;&#12377;&#12427;&#12487;&#12540;&#12479;&#21517;&#12434;&#25351;&#23450;:aesthetics&#12458;&#12503;&#12471;&#12519;&#12531;
TestPlot +
  facet_wrap(~ SubGroup, scales = "free_x")+
  scale_colour_multi(
    aesthetics = c("SG1", "SG2", "SG3"),
    colours = list(c("black", "green"),
                   c("white", "blue"),
                   c("red", "blue")),
    guide = list(guide_colourbar(barheight = unit(35, "pt")))
  )

#&#20961;&#20363;&#12434;&#25991;&#23383;&#12398;&#33394;&#12391;&#34920;&#31034;&#12377;&#12427;:scale_colour_brewer/scale_fill_brewer&#12467;&#12510;&#12531;&#12489;
#guides&#12467;&#12510;&#12531;&#12489;&#12395;&#36969;&#24540;&#12377;&#12427;guide_stringlegend&#12467;&#12510;&#12531;&#12489;&#12418;&#12354;&#12426;&#12414;&#12377;
TestPlot +
  scale_colour_brewer(palette = "Dark2",
                      guide = "stringlegend")

###scale_x/y_continuous&#12467;&#12510;&#12531;&#12489;&#12395;&#36969;&#24540;&#12375;&#12390;&#36600;&#12398;&#20307;&#35009;&#12434;&#25972;&#12360;&#12427;#####
TestPlot +
  scale_x_continuous(
    #&#36600;&#12398;&#26360;&#24335;&#12434;&#25351;&#23450;:guide_axis&#12467;&#12510;&#12531;&#12489;
    #sec.axis&#12458;&#12503;&#12471;&#12519;&#12531;&#12392;&#21516;&#26178;&#20351;&#29992;&#12399;&#19981;&#21487;
    #&#20301;&#32622;:position&#12458;&#12503;&#12471;&#12519;&#12531;;"top","bottom","left","right"
    #&#12521;&#12505;&#12523;&#12398;&#34892;/&#21015;&#25968;&#12434;&#25351;&#23450;:n.dodge&#12458;&#12503;&#12471;&#12519;&#12531;
    #guide = guide_axis(position = "top", angle = 90,
    #                   n.dodge = 3),
    
    #&#31532;2&#36600;&#12434;&#25351;&#23450;:dup_axis&#12467;&#12510;&#12531;&#12489;
    sec.axis = dup_axis(breaks = seq(from = 8, to = 45, by = 5),
                        guide = guide_axis(angle = 45)),
    
    #&#36600;&#12398;&#33394;&#12434;&#35373;&#23450;:guide_axis_color&#12467;&#12510;&#12531;&#12489;
    #&#12300;guides&#12301;&#12467;&#12510;&#12531;&#12489;&#12398;&#35373;&#23450;&#12377;&#12427;guide_axis_truncated&#12467;&#12510;&#12531;&#12489;&#12418;&#12354;&#12426;&#12414;&#12377;
    #&#36600;&#12398;&#25551;&#20889;&#31684;&#22258;&#12434;&#25351;&#23450;:trunc_lower/trunc_upper&#12458;&#12503;&#12471;&#12519;&#12531;
    guide = guide_axis_color(color = "red",
                             trunc_lower = unit(c(0.1, 0.3), "npc"),
                             trunc_upper = unit(c(0.2, 0.8), "npc"))
  ) 
########

出力例

・facet_wrap2コマンド

・facet_grid2コマンド

・facet_nestedコマンド

・facet_manualコマンド

・strip_themedコマンド

・force_panelsizesコマンド

・scale_colour_multiコマンド

・guide_axisコマンド


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

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