Rで解析:データ形式と「ggplot2」パッケージのプロット例

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

「 1つの変数:連続変数 」、「2つの変数: 連続変数、離散変数の組み合わせ」による「ggplot2」パッケージでのプロット例です。各データ例のコマンドも紹介していますので、プロットに対するデータ形式の参考にしてください。

なお、「ggplot2」パッケージ を利用するために「tidyverse」パッケージを使用します。

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

スポンサーリンク

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

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

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

実行コマンド

・「tidyverse」パッケージの読み込みとデータ例の作成

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

###データ例の作成#####
set.seed(1234)
n <- 300
TestData <- tibble(Group = sample(paste0("Group", 1:4), n,
                                  replace = TRUE),
                   X_num_Data = sample(c(1:50), n, replace = TRUE),
                   Y_num_Data = sample(c(51:100), n, replace = TRUE),
                   Chr_Data = sample(c("&#12363;", "&#12425;", "&#12384;", "&#12395;",
                                         "&#12356;", "&#12356;", "&#12418;", "&#12398;"),
                                       n, replace = TRUE),
                   Fct_Data = factor(sample(c("&#12363;", "&#12425;", "&#12384;", "&#12395;",
                                              "&#12356;", "&#12356;", "&#12418;", "&#12398;"),
                                            n, replace = TRUE)))

#&#30906;&#35469;
TestData
# A tibble: 300 x 5
   Group  X_num_Data Y_num_Data Chr_Data Fct_Data
   <chr>       <int>      <int> <chr>    <fct>   
1 Group4         31         83 &#12384;       &#12356;      
2 Group4         33         95 &#12363;       &#12395;      
3 Group2         45         74 &#12425;       &#12356;      
4 Group2         10         99 &#12356;       &#12356;      
5 Group1         22         90 &#12398;       &#12384;      
6 Group4         13         65 &#12356;       &#12395;      
7 Group3         27         76 &#12356;       &#12425;      
8 Group1         40         66 &#12384;       &#12425;      
9 Group1         18         92 &#12356;       &#12418;      
10 Group2         23         84 &#12398;       &#12418;    
#######

・1つの変数:連続変数で作成できるプロット

#&#22522;&#26412;&#12392;&#12394;&#12427;ggplot2&#12398;&#12487;&#12540;&#12479;&#20316;&#25104;
One_Cotinuous <- ggplot(TestData, aes(x = X_num_Data,
                                      color = Group,
                                      fill = Group))
  
#geom_area&#12467;&#12510;&#12531;&#12489;
#&#38598;&#35336;&#26041;&#27861;&#12434;&#35373;&#23450;:stat&#12458;&#12503;&#12471;&#12519;&#12531;;"bin","count","density"&#12394;&#12393;&#12364;&#12354;&#12426;&#12414;&#12377;
One_Cotinuous +
  geom_area(stat = "count", alpha = 0.7) +
  scale_fill_manual(values = c("#a87963", "#505457",
                               "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_area")

#geom_density&#12467;&#12510;&#12531;&#12489;
One_Cotinuous +
  geom_density(alpha = 0.7) +
  scale_fill_manual(values = c("#a87963", "#505457",
                               "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_density")

#geom_dotplot&#12467;&#12510;&#12531;&#12489;
#&#12499;&#12531;&#12398;&#34920;&#31034;&#25163;&#27861;:method&#12458;&#12503;&#12471;&#12519;&#12531;;"dotdensity","histodot"
One_Cotinuous +
  geom_dotplot(method = "dotdensity", binwidth = 5, alpha = 0.9) +
  scale_fill_manual(values = c("#a87963", "#505457",
                               "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_dotplot")

#geom_freqpoly&#12467;&#12510;&#12531;&#12489;
One_Cotinuous +
  geom_freqpoly(binwidth = 5, size = 2) +
  scale_color_manual(values = c("#a87963", "#505457",
                                "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_freqpoly")

#geom_histogram&#12467;&#12510;&#12531;&#12489;
One_Cotinuous +
  geom_histogram(binwidth = 5) +
  scale_fill_manual(values = c("#a87963", "#505457",
                               "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_histogram")

#geom_qq&#12467;&#12510;&#12531;&#12489;
ggplot(TestData) +
  geom_qq(aes(sample = X_num_Data, colour = Group)) +
  scale_fill_manual(values = c("#a87963", "#505457",
                               "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_qq")

#geom_ribbon&#12467;&#12510;&#12531;&#12489;
One_Cotinuous +
  geom_ribbon(aes(ymin = X_num_Data - 10, ymax = X_num_Data + 10),
              alpha = 0.3) +
  scale_fill_manual(values = c("#a87963", "#505457",
                               "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_ribbon")

【出力例】

・2つの変数:x、y共に連続変数 で作成できるプロット

#&#22522;&#26412;&#12392;&#12394;&#12427;ggplot2&#12398;&#12487;&#12540;&#12479;&#20316;&#25104;
Two_Cotinuous <- ggplot(TestData, aes(x = X_num_Data,
                                      y = Y_num_Data,
                                      color = Group,
                                      fill = Group))

#geom_point&#12467;&#12510;&#12531;&#12489;
#&#12471;&#12531;&#12508;&#12523;&#12434;&#25351;&#23450;:shape&#12458;&#12503;&#12471;&#12519;&#12531;;0:25&#12414;&#12391;&#25351;&#23450;&#21487;&#33021;
#&#20363;&#12391;&#12399;&#20840;&#31278;&#39006;&#12503;&#12525;&#12483;&#12488;
Two_Cotinuous +
  geom_point(alpha = 1, size = 4, shape = rep(0:25, length = 300)) +
  scale_fill_manual(values = c("#a87963", "#505457",
                               "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_point")

#geom_rug&#12467;&#12510;&#12531;&#12489;
Two_Cotinuous +
  geom_rug(size = 1.5) +
  scale_fill_manual(values = c("#a87963", "#505457",
                               "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_rug")

#geom_smooth&#12467;&#12510;&#12531;&#12489;
Two_Cotinuous +
  geom_smooth() +
  scale_fill_manual(values = c("#a87963", "#505457",
                               "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_smooth")

#geom_label&#12467;&#12510;&#12531;&#12489;
Two_Cotinuous +
  geom_label(aes(label = Group)) +
  scale_fill_manual(values = c("#a87963", "#505457",
                               "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_label")

#geom_text&#12467;&#12510;&#12531;&#12489;
Two_Cotinuous +
  geom_text(aes(label = Group)) +
  scale_fill_manual(values = c("#a87963", "#505457",
                               "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_text")

【出力例】

・2つの変数:離散変数、連続変数の組み合わせで作成できるプロット

#&#22522;&#26412;&#12392;&#12394;&#12427;ggplot2&#12398;&#12487;&#12540;&#12479;&#20316;&#25104;
#&#12503;&#12525;&#12483;&#12488;&#12364;&#35211;&#12420;&#12377;&#12367;&#12394;&#12427;&#12424;&#12358;&#12487;&#12540;&#12479;&#12434;100&#12414;&#12391;&#20351;&#29992;
Discrete_Cotinuous <- ggplot(TestData[1:100,],
                             aes(x = Group, y = X_num_Data,
                                 color = Group, fill = Group))

#geom_violin&#12467;&#12510;&#12531;&#12489;
Discrete_Cotinuous +
  geom_violin() +
  scale_fill_manual(values = c("#a87963", "#505457",
                               "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_violin")

#geom_boxplot&#12467;&#12510;&#12531;&#12489;
Discrete_Cotinuous +
  geom_boxplot() +
  scale_fill_manual(values = c("#a87963", "#505457",
                               "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_boxplot")

#geom_dotplot&#12467;&#12510;&#12531;&#12489;
#&#12489;&#12483;&#12488;&#12398;&#34920;&#31034;&#26041;&#27861;:stackdir&#12458;&#12503;&#12471;&#12519;&#12531;;"up","down","center","centerwhole"
Discrete_Cotinuous +
  geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 5) +
  scale_fill_manual(values = c("#a87963", "#505457",
                               "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_dotplot")

#geom_point&#12467;&#12510;&#12531;&#12489;
Discrete_Cotinuous +
  geom_point(size = 1) +
  scale_fill_manual(values = c("#a87963", "#505457",
                               "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_point")

【出力例】

・2つの変数:x、y共に離散変数の組み合わせで作成できるプロット

#&#22522;&#26412;&#12392;&#12394;&#12427;ggplot2&#12398;&#12487;&#12540;&#12479;&#20316;&#25104;
Two_Discrete <- ggplot(TestData, aes(x = Chr_Data,
                                     y = Fct_Data,
                                     color = Group,
                                     fill = Group))
#geom_count&#12467;&#12510;&#12531;&#12489;
Two_Discrete +
  geom_count() +
  scale_color_manual(values = c("#a87963", "#505457",
                                "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_count")

#geom_jitter&#12467;&#12510;&#12531;&#12489;
Two_Discrete +
  geom_jitter(size = 3) +
  scale_color_manual(values = c("#a87963", "#505457",
                                "#4b61ba", "#A9A9A9")) +
  labs(title = "geom_jitter")

【出力例】


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

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