![見出し画像](https://assets.st-note.com/production/uploads/images/110639935/rectangle_large_type_2_4454cf2fc1a5fe47b9e515efb008a10f.png?width=800)
ボックスプロットに色付け
ggplot2 を使って作成したボックスプロットに色付けする方法を紹介します。
ボックスに色付け: aes()に fill を指定
ボックスプロットのボックスを、サンプルごとに色を変えて色付けするには、ggplot() で使用するデータを指定する際に、aes() の中で、 「fill = sample」 と指定します。
plot_data <- input_data %>%
gather(Sample1, Sample2, key = "sample", value = "read_count")
ggplot(plot_data, aes(x = sample, y = read_count, fill = sample)) +
geom_boxplot()
下記のようにサンプルごとに異なる色で表示されます。色は自動的に選ばれます。
![](https://assets.st-note.com/img/1689175445216-v58XjPNODn.png?width=800)
なお、 fill の代わりに、「color = sample」と、color を指定した場合は、ボックスプロットの外側が色付けされます。
ggplot(plot_data, aes(x = sample, y = read_count, color = sample)) +
geom_boxplot()
![](https://assets.st-note.com/img/1689176211477-3yzrnKip3K.png?width=800)
この記事が気に入ったらサポートをしてみませんか?