ggplot2: マニュアルで色を指定する

aes() オブジェクトに color または fill を指定すると、グループに応じて色付けできます。このとき、色をマニュアルで指定するには、 scale_color_manual() または scale_fill_manual() を用います。 ggplot の aes() に fill を指定したのち、geom_boxplot() のあとに、プラス (+) で続けて指定します。

scale_fill_manual(values = c(色1, 色2, ...))

コードの例:

g <- ggplot(plot_data, aes(x = sample, y = read_count, fill = sample))
g + geom_boxplot() + scale_fill_manual(values = c("salmon", "royalblue"))
マニュアルで色を指定した例

複数の色を指定するには、予め定義されたパレットを用いると便利です。 RColorBrewer のパレットを使える scale_fill_brewer() 関数があります。

g + geom_boxplot() + scale_fill_brewer(palette = "Spectral")
scale_fill_brewer の例。

brewer のパレットに指定できる色の例です。YlOrRd, YlOrBr, Set1, Set2, Dark2, Spectral などがあります。(文字列として指定するので、ダブルクオーテーションで囲むのを忘れずに。)

RColorBrewer より。

scale_color_manual, scale_fill_manual()

https://ggplot2.tidyverse.org/reference/scale_manual.html
 

ggplot2: ボックスプロットに色を付ける

ggplot2 では、あるパラメーターごとに色付けすることが簡単にできます。色付けするには、aes() の指定時、 color にパラメーターを指定します。サンプルごとに異なる色を付けるには、下記のように指定します。

g <- ggplot(plot_data, aes(x = sample, y = read_count, color = sample))

自動的に選択された色で、色づけされます。

一方、ボックスプロットの枠ではなく、中身を色付けしたい場合は、 color ではなく、 fill で指定します。

g <- ggplot(plot_data, aes(x = sample, y = read_count, fill = sample))

コードの例:

input_data <- tibble("Sample1" = rnorm(100),
                     "Sample2" = rnorm(100))

plot_data <- input_data %>%
  gather(Sample1, Sample2, key = "sample", value = "read_count")

g <- ggplot(plot_data, aes(x = sample, y = read_count, fill = sample))
g + geom_boxplot()
ggsave("boxplot_image_fill.png", width = 4, height = 4, unit = "in")
 

ggplot2: ボックスプロットにドットプロットを重ねる

ggplot2 を使うと、ボックスプロットに、ドットプロットを重ねて描画することもできます。バイオリンプロットのときと同様に、 geom_boxplot()geom_dotplot()+ で続けて指定します。

g + geom_boxplot() + geom_dotplot()

g は、ggplot のオブジェクトです。具体的には、下記のようなコードです。

input_data <- tibble("Sample1" = rnorm(100),
                     "Sample2" = rnorm(100))

plot_data <- input_data %>%
  gather(Sample1, Sample2, key = "sample", value = "read_count")

g <- ggplot(plot_data, aes(x = sample, y = read_count))

g + geom_boxplot() +
  geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 0.1)

下記のようなイメージが出力されます。

ボックスプロットにドットプロットを重ねた例。

なお、ドットを整列させない場合は、 geom_jitter() 関数を用います。

g + geom_boxplot() + geom_jitter()

ドットが横方向にランダムに描画されます。ランダムな結果なので、実行するたびにドットの位置が変化します。(見た目だけの問題なので、ばらつきに意味はありません。)geom_point(position = “jitter”) でも同様です。

geom_jitter の例。

 

ggplot2: バイオリンプロット (geom_violin)

データ数が多い場合、データの分布が見るときに、バイオリンプロット geom_violin() を使います。バイオリンプロットでは、データの分布が分布曲線の波形で表示されるので、どの区間に多くデータが存在しているか確認できます。データの分布が内部で大きく2つに分かれているようなケースではも、文字通りバイオリンの形になります。

geom_violin(trim = FALSE)

波形の上端と下端が切り取られたイメージにしたくない場合は、オプションで trim = FALSE を指定します。プロットするためのデータは、 geom_boxplot(), geom_dotplot() と同じです。(例のオブジェクトg までは同じ。)

input_data <- tibble("Sample1" = rnorm(100),
                     "Sample2" = rnorm(100))

plot_data <- input_data %>%
  gather(Sample1, Sample2, key = "sample", value = "read_count")

g <- ggplot(plot_data, aes(x = sample, y = read_count))
g + geom_violin(trim = FALSE)

また、ggplot2 は、プロットを重ねて書くこともできます。 geom_violin() + geom_dotplot() とすることで、バイオリンプロットにドットプロットを重ねて表示できます。

g + geom_violin(trim = FALSE) +
  geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 0.1)

 

ggplot2: ドットプロット (geom_dotplot)

データ数がそれほど多くない場合は、ボックスプロットに代わり、ドットプロットが用いられることもあります。ドットプロットは、データの数だけ、点(ドット)を表示するので、データの広がりをイメージしやすいでしょう。

geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 0.1)

ドットプロットを書くには、geom_dotplot() を使います。ボックスプロットのようにサンプルを横に並べるには、binaxis = “y” を指定します。また、ドットを左右対象に並べるには、 stackdir = “center” を指定します。 binwidth は、ドットを並べる区画の幅です。ドットの大きさにも影響します。(binwidth は、何回か描画して、試行錯誤することになると思います。)

ggplot オブジェクト (=g) を作るまでは、ボックスプロットと同じです。g を定義しておくと、geom_boxplot() を geom_dotplot() に変更するだけで描けます。

input_data <- tibble("Sample1" = rnorm(100),
                     "Sample2" = rnorm(100))

plot_data <- input_data %>%
  gather(Sample1, Sample2, key = "sample", value = "read_count")

g <- ggplot(plot_data, aes(x = sample, y = read_count))
g + geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 0.1)
ドットプロットの例