いろいろ倉庫

KNIME、EXCEL、R、Pythonなどの備忘録

【R】グラフを並べたい(patchwork)

・お題:最近、ggplot2を使っていろいろグラフを作成した。これらを並べてくっつけたい。

 

・ggplot2で作成したグラフを並べるパッケージとして、patchworkというのがある。以下を参考に、使い方をメモしたい。正しいことは以下のサイトをご確認いただきたい。

gotellilab.github.io

・適当にデータセットを作成する。

>library(tidyverse)

> set.seed(1)
> Compound <- rep(c("A", "B"),
+             each = 40)
> Concentration <- rep(rep(c(0, 1, 3, 10), each = 10), times = 2)
> Score <- c(rnorm(10, mean = 10, sd = 3),
+            rnorm(10, mean = 15, sd = 3),
+            rnorm(10, mean = 30, sd = 3),
+            rnorm(10, mean = 35, sd = 3),
+            rnorm(10, mean = 10, sd = 3),
+            rnorm(10, mean = 20, sd = 3),
+            rnorm(10, mean = 35, sd = 3),
+            rnorm(10, mean = 35, sd = 3))
> df <- data.frame("Compound" = factor(Compound),
+                  "Concentration" = factor(Concentration),
+                  "Score" = Score) %>% 
+   group_by(Compound, Concentration)

 

・いくつかグラフを作成する。

> #Box Plot
> box <- ggplot(data = df) +
+   geom_boxplot(mapping = aes(x = Concentration, y = Score, fill = Compound),
+                color = "gray30", #線の色
+                size = 1, #線の太さ
+                alpha = 0.5) +
+   ylim(0, 45) +
+   labs(title="Box Plot",
+        x = "Concentration(uM)",
+        y = "Score")+
+   theme_classic()
> plot(box)

> #Line Plot
> mean.df <- df %>% summarise(., Average = mean(Score))
`summarise()` has grouped output by 'Compound'. You can override using the `.groups` argument.
> line <- ggplot(data = mean.df) +
+   geom_line(mapping = aes(x = Concentration, y = Average, color = Compound, group = Compound), #X軸、Y軸、色の基準
+             size = 1, #プロットの大きさ
+             linetype = "solid", 
+             alpha = 0.5) + #透明度
+   ylim(0, 45) + #Y軸の表示の範囲
+   labs(title="Line Plot", #表題
+        x = "Concentration(uM)",
+        y = "Score")+
+   theme_classic()
> plot(line)

> #Bar Plot
> bar <- 
+   ggplot(data = df,
+          mapping = aes(x = Concentration,
+                        y = Score,
+                        fill = Compound))+
+   geom_bar(stat = "summary",
+            fun = "mean",
+            color = "gray30",
+            width = 0.6,
+            position = position_dodge2(preserve = "single"))+ #カラムの幅を揃える
+   geom_point(mapping = aes(x = Concentration, 
+                            y = Score,
+                            fill = Compound),
+              position = position_dodge(0.6),
+              color = "gray30") +
+   scale_y_continuous(expand = c(0, 0), limits = c(0, 45)) +
+   labs(title="Bar Plot",
+        x = "Concentration(uM)",
+        y = "Score") +
+   theme_classic() +
+   facet_grid(. ~ Compound,
+              scales = "free_x",
+              space = "free_x",
+              switch = "x")+
+   theme(strip.placement = "outside")
> plot(bar)
> plot(bar)

> #Pie Chart
> df2 <- data.frame(Group = c("X", "Y", "Z"), Score = c(0.5, 0.3, 0.2))
> pie <- ggplot(data = df2, mapping = aes(x = "", y = Score, fill = reorder(Group, Score)))+
+   geom_bar(width = 1, stat = "identity")+
+   geom_text(aes(label = paste0(Score*100, "%")), 
+             position = position_stack(vjust = 0.5),
+             color = "white") +
+   labs(x = "", y = "", title = "Pie Chart") +
+   coord_polar("y", start = 0) +
+   theme_classic()+
+   theme(axis.line = element_blank(),
+         axis.text = element_blank())
> plot(pie)

・次に、patchworkで並べてみる。|で繋ぐと横に並ぶ。

> library(patchwork)
> line | pie

・/で繋ぐと縦に並ぶ。

> line / pie

・組み合わせると、縦横にくっつけることができる。

> (line | pie) / bar

・組み合わせてネストすることもできる。

> (box | (pie / line)) / bar

・お弁当箱におかずを詰めているようで楽しいが、見づらくならないようにしなければ。。。

 

おわり