Rでコマンド:ggplot2での色指定方法のまとめ

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

忘れがちなggplot2でのcolとfillの色指定方法をザクッとまとめました。軸に設定した離散値、連続値を意識すると理解しやすいのではと思います。

tidyverseのバージョンは1.3.1。windows 11のR version 4.1.2で動作を確認しています。


スポンサーリンク

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

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

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

色指定方法のまとめ

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

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

###準備#####
#データ例の作成
n <- 300
PlotData <- data.frame(ID = rep(1:5, len = n),
                       Group = sample(c("A", "B", "C"), n, replace = TRUE),
                       Time_A = abs(rnorm(n)), Time_B = -log2(abs(rnorm(n))))

#Group&#24773;&#22577;&#12391;&#33394;&#12434;&#20184;&#19982;
PlotData <- PlotData %>% 
  #case_when&#12399;&#26465;&#20214;&#24335; ~ &#32080;&#26524;&#12391;&#25351;&#23450;&#12375;&#12414;&#12377;
  mutate(GroupCol = case_when(Group == "A" ~ "#a0b981",
                              Group == "B" ~ "#47547c",
                              Group == "C" ~ "#9f8288"))

#&#33394;&#12398;&#30906;&#35469; #https://www.karada-good.net/analyticsr/r-109
#install.packages("scales")
#library("scales")
#show_col(c("#a0b981", "#47547c", "#9f8288"))

#&#12503;&#12525;&#12483;&#12488;&#12434;&#20006;&#12409;&#12390;&#34920;&#31034;&#12377;&#12427;&#12398;&#12395;&#20415;&#21033;&#12394;&#12497;&#12483;&#12465;&#12540;&#12472;
#https://www.karada-good.net/analyticsr/r-591
#install.packages("multipanelfigure")
library("multipanelfigure")
########

###&#12487;&#12540;&#12479;&#12395;&#33394;&#24773;&#22577;&#12364;&#21547;&#12414;&#12428;&#12390;&#12356;&#12427;&#22580;&#21512;:scale_XXX_identity&#12467;&#12510;&#12531;&#12489;#####
#col&#12434;&#25351;&#23450;&#12377;&#12427;&#22580;&#21512;:scale_colour_identity&#12467;&#12510;&#12531;&#12489;
PointPlot <- ggplot(PlotData, aes(x = Time_A, y = Time_B)) +
  geom_point(aes(col = GroupCol), size = 2) +
  scale_colour_identity(guide = "none") +
  labs(title = "scale_colour_identity")
#fill&#12434;&#25351;&#23450;&#12377;&#12427;&#22580;&#21512;:scale_fill_identity&#12467;&#12510;&#12531;&#12489;
BarPlot <- ggplot(PlotData, aes(x = Group)) +
  geom_bar(aes(fill = GroupCol)) +
  scale_fill_identity(guide = "none") +
  labs(title = "scale_fill_identity") 

#&#12503;&#12525;&#12483;&#12488;
multi_panel_figure(columns = 2, rows = 1) %>%
  fill_panel(panel = PointPlot, column = 1, row = 1, label = "") %>%
  fill_panel(panel = BarPlot, column = 2, row = 1, label = "")
########

###&#33394;&#24773;&#22577;&#12434;&#25351;&#23450;&#12377;&#12427;:scall_XXX_manual&#12467;&#12510;&#12531;&#12489;#####
#col&#12398;&#22580;&#21512;:scale_colour_manual&#12467;&#12510;&#12531;&#12489;
PointPlot <- ggplot(PlotData, aes(x = Time_A, y = Time_B)) +
  geom_point(aes(col = Group), size = 2) +
  scale_colour_manual(values = c("#deb7a0", "#4b61ba", "#a87963"), guide = "none") +
  labs(title = "scale_colour_manual")

#fill&#12398;&#22580;&#21512;:scale_fill_manual&#12467;&#12510;&#12531;&#12489;
BarPlot <- ggplot(PlotData, aes(x = Group)) +
  geom_bar(aes(fill = Group)) +
  scale_fill_manual(values = c("#deb7a0", "#4b61ba", "#a87963"), guide = "none") +
  labs(title = "scale_fill_manual")

#&#12503;&#12525;&#12483;&#12488;
multi_panel_figure(columns = 2, rows = 1) %>%
  fill_panel(panel = PointPlot, column = 1, row = 1, label = "") %>%
  fill_panel(panel = BarPlot, column = 2, row = 1, label = "")
########

###&#12464;&#12521;&#12472;&#12456;&#12531;&#12488;&#21270;:scall_XXX_gradient,scall_XXX_gradient2&#12467;&#12510;&#12531;&#12489;#####
#col&#12398;&#22580;&#21512;:scale_colour_gradient&#12467;&#12510;&#12531;&#12489;
#col&#12395;&#25351;&#23450;&#12377;&#12427;&#12487;&#12540;&#12479;&#12399;&#36899;&#32154;&#22793;&#25968;
PointPlot1 <- ggplot(PlotData, aes(x = Time_A, y = Time_B)) +
  geom_point(aes(col = Time_A), size = 2) +
  scale_colour_gradient(low = "#6f74a4", high = "#f6adad", guide = "none") +
  labs(title = "scale_colour_gradient")

#col&#12398;&#22580;&#21512;:scale_colour_gradient2&#12467;&#12510;&#12531;&#12489;
#col&#12395;&#25351;&#23450;&#12377;&#12427;&#12487;&#12540;&#12479;&#12399;&#36899;&#32154;&#22793;&#25968;
PointPlot2 <- ggplot(PlotData, aes(x = Time_A, y = Time_B)) +
  geom_point(aes(col = Time_A), size = 2) +
  scale_colour_gradient2(low = "#6f74a4", high = "#f6adad", mid = "#7edbf2", guide = "none") +
  labs(title = "scale_colour_gradient2")

#fill&#12398;&#22580;&#21512;:scale_fill_gradient&#12467;&#12510;&#12531;&#12489;
histogram1 <- ggplot(PlotData, aes(x = Time_A)) +
  geom_histogram(aes(fill = ..x..)) +
  scale_fill_gradient(low = "#6f74a4", high = "#f6adad", guide = "none") +
  labs(title = "scale_fill_gradient")

#fill&#12398;&#22580;&#21512;:scale_fill_gradient2&#12467;&#12510;&#12531;&#12489;
histogram2 <- ggplot(PlotData, aes(x = Time_A)) +
  geom_histogram(aes(fill = ..x..)) +
  scale_fill_gradient2(low = "#6f74a4", high = "#f6adad", mid = "#7edbf2", guide = "none") +
  labs(title = "scale_fill_gradient2")

#&#12503;&#12525;&#12483;&#12488;
multi_panel_figure(columns = 2, rows = 2) %>%
  fill_panel(panel = PointPlot1, column = 1, row = 1, label = "") %>%
  fill_panel(panel = PointPlot2, column = 2, row = 1, label = "") %>%
  fill_panel(panel = histogram1, column = 1, row = 2, label = "") %>%
  fill_panel(panel = histogram2, column = 2, row = 2, label = "")
########

###&#33394;&#12497;&#12524;&#12483;&#12488;&#12434;&#21033;&#29992;:scall_XXX_brewer,scall_XXX_distiller&#12467;&#12510;&#12531;&#12489;#####
#&#33394;&#12497;&#12524;&#12483;&#12488;&#12399;&#27425;&#12398;&#12467;&#12510;&#12531;&#12489;&#12391;&#30906;&#35469;:RColorBrewer::display.brewer.all()
#col&#12398;&#22580;&#21512;:scale_colour_brewer&#12467;&#12510;&#12531;&#12489;
PointPlot1 <- ggplot(PlotData, aes(x = ID, y = Time_B)) +
  geom_point(aes(col = Group), size = 2) +
  scale_colour_brewer(palette = "Set3", guide = "none") +
  labs(title = "scale_colour_brewer")

#col&#12398;&#22580;&#21512;:scale_colour_distiller&#12467;&#12510;&#12531;&#12489;
PointPlot2 <- ggplot(PlotData, aes(x = ID, y = Time_B)) +
  geom_point(aes(col = ..x..), size = 2) +
  scale_colour_distiller(palette = "Set3", guide = "none") +
  labs(title = "scale_colour_distiller")

#fill&#12398;&#22580;&#21512;:scale_fill_brewer&#12467;&#12510;&#12531;&#12489;
histogram1 <- ggplot(PlotData, aes(x = Group)) +
  geom_histogram(aes(fill = Group), stat="count") +
  scale_fill_brewer(palette = "Set3", guide = "none") +
  labs(title = "scale_fill_brewer")

#fill&#12398;&#22580;&#21512;:scale_fill_distiller&#12467;&#12510;&#12531;&#12489;
histogram2 <- ggplot(PlotData, aes(x = Time_A)) +
  geom_histogram(aes(fill = ..x..)) +
  scale_fill_distiller(palette = "Set3", guide = "none") +
  labs(title = "scale_fill_distiller")

#&#12503;&#12525;&#12483;&#12488;
multi_panel_figure(columns = 2, rows = 2) %>%
  fill_panel(panel = PointPlot1, column = 1, row = 1, label = "") %>%
  fill_panel(panel = PointPlot2, column = 2, row = 1, label = "") %>%
  fill_panel(panel = histogram1, column = 1, row = 2, label = "") %>%
  fill_panel(panel = histogram2, column = 2, row = 2, label = "")
########

###&#12464;&#12524;&#12540;&#12473;&#12465;&#12540;&#12523;&#21270;:scale_XXX_grey&#12467;&#12510;&#12531;&#12489;#####
#col&#12398;&#22580;&#21512;:scale_colour_grey&#12467;&#12510;&#12531;&#12489;
PointPlot <- ggplot(PlotData, aes(x = Time_A, y = Time_B)) +
  geom_point(aes(col = Group), size = 2) +
  scale_colour_grey(start = .2, end = .7, guide = "none") +
  labs(title = "scale_colour_grey")

#fill&#12398;&#22580;&#21512;:scale_fill_grey&#12467;&#12510;&#12531;&#12489;
BarPlot <- ggplot(PlotData, aes(x = Group)) +
  geom_bar(aes(fill = Group)) +
  scale_fill_grey(start = .2, end = .7, guide = "none") +
  labs(title = "scale_fill_grey")

#&#12503;&#12525;&#12483;&#12488;
multi_panel_figure(columns = 2, rows = 1) %>%
  fill_panel(panel = PointPlot, column = 1, row = 1, label = "") %>%
  fill_panel(panel = BarPlot, column = 2, row = 1, label = "")
########



出力例

データに色情報が含まれている場合
・scale_XXX_identityコマンド

色情報を指定する
・scall_XXX_manualコマンド


グラジエント化
・scall_XXX_gradient,scall_XXX_gradient2コマンド

色パレットを利用
・scall_XXX_brewer,scall_XXX_distillerコマンド

グレースケール化
・scale_XXX_greyコマンド


少しでも、あなたのウェブや実験の解析が楽になりますように!!

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