Rで解析:ggplot2の周囲にグラフの追加が簡単です!!「ggside」パッケージ

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

ggplot2の周囲にグラフの追加が簡単なパッケージの紹介です。非常に使い方が簡単です。追加可能なグラフのコマンドは

「geom_*freqpoly」、「geom_*sidebar」、「geom_*sideboxplot」、「geom_*sidedensity」、「geom_*sidehistogram」、「geom_*sideline」、「geom_*sidepoint」、「geom_*sidetile」、「geom_*sideviolin」

などです。なお、*には「xまたはy」が入ります。また、グラフを「facet_grid」コマンドで分割すると各周辺のグラフも分割されてしまいます。しかし、本パッケージに収録されている「ggside」コマンドを利用するとグラフの分割を調整することが可能です。大変便利な機能だと思います。

パッケージバージョンは0.2.1。実行コマンドはwindows 11のR version 4.2.1で確認しています。

スポンサーリンク

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

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

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

実行コマンド

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

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

###データ例の作成#####
#tidyverseパッケージがなければインストール
if(!require("tidyverse", quietly = TRUE)){
  install.packages("tidyverse");require("tidyverse")
}
set.seed(1234)
n <- 100
TestData <- tibble(Group = sample(paste0("Group", 1:5), n, replace = TRUE),
                   Char_Data = sample(LETTERS, n, replace = TRUE),
                   X_Data = sample(c(1:50), n, replace = TRUE),
                   Y_Data = sample(c(51:100), n, replace = TRUE)) %>%
  arrange(Group, Char_Data) 
########

###HeatMap&#12395;&#12487;&#12540;&#12479;&#12434;&#36861;&#21152;&#12377;&#12427;#####
#&#12487;&#12540;&#12479;&#12398;&#28310;&#20633;
HeatData <- TestData %>%
  group_by(Group, Char_Data) %>%
  summarise(X_Data_Mean = mean(X_Data), .groups = "drop") %>%
  group_by(Char_Data) %>%
  mutate(Char_Mean = mean(X_Data_Mean), .keep = "all") %>%

#&#22522;&#26412;&#12503;&#12525;&#12483;&#12488;&#12398;&#20316;&#25104;:ggplot2::geom_tile&#12467;&#12510;&#12531;&#12489;
ggplot(HeatData, aes(x = Group, y = Char_Data)) +
  geom_tile(aes(fill = X_Data_Mean)) +
  scale_fill_viridis_b() -> HeatPlot
#&#30906;&#35469;
HeatPlot

###HeatPlot&#12458;&#12502;&#12472;&#12455;&#12463;&#12488;&#12395;&#12464;&#12521;&#12501;&#12434;&#36861;&#21152;###
HeatPlot +
  #x&#36600;&#20596;&#19978;&#37096;&#12395;BoxPlot&#12434;&#36861;&#21152;:geom_xsideboxplot&#12467;&#12510;&#12531;&#12489;
  #&#22615;&#33394;:xfill&#12458;&#12503;&#12471;&#12519;&#12531;;fill&#12391;&#12399;&#12394;&#12356;
  geom_xsideboxplot(aes(x = Group, y = X_Data_Mean,
                        xfill = Group), show.legend = FALSE) +
  
  #BoxPlot&#12398;y&#36600;&#12434;&#35519;&#25972;:scale_xsidey_continuous&#12467;&#12510;&#12531;&#12489;
  scale_xsidey_continuous(minor_breaks = NULL, limits = c(0,NA)) +
  
  #y&#36600;&#20596;&#21491;&#20596;&#12395;HeatMap&#12434;&#36861;&#21152;:geom_ysidetile&#12467;&#12510;&#12531;&#12489;
  #&#22615;&#33394;:yfill&#12458;&#12503;&#12471;&#12519;&#12531;;fill&#12391;&#12399;&#12394;&#12356;
  geom_ysidetile(aes(x = "mean", yfill = Char_Mean)) +
  
  #y&#36600;&#20596;&#12398;HeatMap&#12398;&#33394;&#12434;&#35373;&#23450;:scale_yfill_gradient&#12467;&#12510;&#12531;&#12489;
  #&#12381;&#12398;&#20182;&#33394;&#12398;&#35373;&#23450;&#12467;&#12510;&#12531;&#12489;&#12399;?"scale_xcolour"&#12391;&#30906;&#35469;&#12375;&#12390;&#12367;&#12384;&#12373;&#12356;
  scale_yfill_gradient(low ="#FFFFFF", high = "#0000FF") +
  
  #&#12486;&#12540;&#12510;&#12391;&#20307;&#35009;&#12434;&#35519;&#25972;
  #&#12381;&#12398;&#20182;&#20307;&#35009;&#12467;&#12510;&#12531;&#12489;&#12399;?"ggside-theme"&#12391;&#30906;&#35469;&#12375;&#12390;&#12367;&#12384;&#12373;&#12356;
  theme(
    #&#12497;&#12493;&#12523;&#12469;&#12452;&#12474;&#12434;&#35519;&#25972;:ggside.panel.scale&#12467;&#12510;&#12531;&#12489;
    #&#20363;&#12391;&#12399;x&#36600;&#12398;BoxPlot
    #&#26411;&#23614;&#12398;".x"&#12434;&#21462;&#12426;&#38500;&#12367;&#12392;&#20001;&#36600;,".y"&#12391;y&#36600;HeatMap&#12469;&#12452;&#12474;
    ggside.panel.scale.x = 0.3,
    
    #&#21508;&#12497;&#12493;&#12523;&#26528;&#32218;&#33394;:ggside.panel.border&#12467;&#12510;&#12531;&#12489;
    ggside.panel.border = element_rect(fill = NA, 
                                       colour = "red", size = 1),
    
    #&#21508;&#12497;&#12493;&#12523;&#12398;&#12513;&#12514;&#12522;&#32218;:ggside.panel.grid&#12467;&#12510;&#12531;&#12489;;&#12381;&#12398;&#20182;&#12395;
    #ggside.panel.grid.major,ggside.panel.grid.minor,
    #ggside.panel.grid.major.x,ggside.panel.grid.major.y,
    #ggside.panel.grid.minor.x, ggside.panel.grid.minor.y
    ggside.panel.grid = element_line(colour = "black", size = .1,
                                     linetype = "dotted"),
    
    #&#21508;&#12497;&#12493;&#12523;&#32972;&#26223;&#33394;:ggside.panel.background&#12467;&#12510;&#12531;&#12489;
    ggside.panel.background = element_blank(),
    
    #&#20197;&#19979;ggplot2&#12398;&#22522;&#26412;&#12467;&#12510;&#12531;&#12489;
    panel.background = element_rect(fill = "grey20"),
    panel.grid.major = element_line(colour = NA)
    ) 
########

###PointPlot&#12395;&#23494;&#24230;&#20998;&#24067;&#12392;BoxPlot&#12434;&#36861;&#21152;#####
ggplot(TestData, aes(X_Data, Y_Data, fill = Group)) +
  geom_point(aes(color = Group)) +
  geom_xsidedensity(alpha = 0.5, position = "stack") +
  geom_ysideboxplot(aes(x = Group), orientation = "x") +
  scale_ysidex_discrete(guide = guide_axis(angle = 30)) +
  theme(ggside.panel.scale = 0.5,
        ggside.panel.border = element_rect(fill = NA,
                                           colour = "red",
                                           size = 0.5,
                                           linetype = "dotted"),
        ggside.panel.background = element_blank()) -> PointTest
#&#30906;&#35469;
PointTest

###&#19978;&#35352;&#12464;&#12521;&#12501;&#12434;&#12300;facet_grid&#12301;&#12467;&#12510;&#12531;&#12489;&#12434;&#36969;&#24540;&#12375;&#12300;ggside&#12301;&#12467;&#12510;&#12531;&#12489;&#12391;x&#20596;&#12398;&#12464;&#12521;&#12501;&#12434;&#38598;&#32004;#####
PointTest +
  facet_grid(.~Group) +
  #&#21508;&#12497;&#12493;&#12523;&#12398;&#20301;&#32622;,&#38598;&#32004;&#12394;&#12393;&#12434;&#35373;&#23450;:ggside&#12467;&#12510;&#12531;&#12489;
  #&#21508;&#12497;&#12493;&#12523;&#12398;&#38598;&#32004;:collapse&#12458;&#12503;&#12471;&#12519;&#12531;
  #"all"&#12391;x,y&#36600;
  ggside(collapse = "y") 

出力例

・HeatPlotオブジェクトにグラフを追加

・PointPlotに密度分布とBoxPlotを追加

・上記グラフを「facet_grid」コマンドを適応し「ggside」コマンドでx側のグラフを集約


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

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