4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RainCloud Plots ~Rを用いた図の描画~

Last updated at Posted at 2020-03-02

RainCloud Plots##

RainCloud Plots は、ボックスプロット、バイオリンプロット、ドットプロットの良いとこ取りをした描画方法で、Rで (Pythonでも) 作成可能です。具体的には、生データ、確率密度、平均値などの要素を冗長性を最小限に抑えた形式で視覚化できます。最後に、論文のURLとgithubのURLをまとめましたので、そちらもご覧ください。

まずは、作図に必要なツール(パッケージ)のインストールを行います。
R のインストールがまだな方は、こちら↓↓
R のインストールに関してわかりやすいサイト
を参考にインストールしてください。

では、早速始めましょう。
RainCloud plots の GitHub page からツール一式をダウンロードします。
このファイルの中の install.R に載っているツールをインストール、使える状態にします。

# ツールのインストーラーのインストール (この作業は1回きりで大丈夫です)
if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install(version = "3.10")
# 必要なツール(パッケージ)のインストール (この作業は1回きりで大丈夫です)
BiocManager::install("tidyverse")
BiocManager::install("rmarkdown")
BiocManager::install("httr")
BiocManager::install("shinydashboard")
BiocManager::install("leaflet")
BiocManager::install("knitr")
BiocManager::install("ggplot2")
BiocManager::install("dplyr")
BiocManager::install("rlibs")
BiocManager::install("caTools")
BiocManager::install("bitops")
BiocManager::install("reshape2") # +α
# 必要なツールを使えるようにする (この作業は R を立ち上げるたびに行いましょう)
library("tidyverse")
library("rmarkdown")
library("httr")
library("shinydashboard")
library("leaflet")
library("knitr")
library("ggplot2")
library("dplyr")
library("rlibs")
library("caTools")
library("bitops")
library("reshape2") # +α

# RainCloud plots を使えるようにします
# GitHub からダウンロードしたフォルダの中の tutorial_R フォルダの R_rainclouds.R を開きます。
# 21 列目から 86 列目までを実行します。

次にデモデータを用意します。
今回は、野生型(WT)、変異体A (MutantA)、変異体B (MutantB) の草丈 (PlantHight) に関しての作成を行いました。

# デモデータの用意
WT <- rnorm(100, mean=20, sd=5)
MutantA <- rnorm(100, mean=10, sd=2)
MutantB <- rnorm(100, mean=30, sd=3)
PlantHight <- data.frame(WT=c(WT), MutantA=c(MutantA), MutantB=c(MutantB))

head(PlantHight) # あたま6行ぶんの表示
        WT   MutantA  MutantB
1 20.81688  7.891188 32.60100
2 23.03243 10.592747 27.01481
3 23.36832  9.899247 30.05136
4 18.66481 10.810630 28.08672
5 26.57562  5.178914 28.19979
6 15.88725 11.279334 29.12107

もし手元に作図したいデータがある場合は、read.table関数(以下参照)などを用いてインプットしてください。EXEL で作成した表は、(私はいつも)タブ区切りのテキストとしてエクスポートしています。今回は、そのタブ区切りのテキストの R へのインプット例を、、、

# デモデータの用意
YOUR_DATA <- read.table("~/あなたのデータがあるディレクトリ/データの名前.txt", header=TRUE, sep="\t")
# header : 1番最初の列をカラムの名前にするかどうか
# sep : 区切り文字は何か(タブ区切りなので "\t")

Raincluod plots は ggplot2 というツールをベースに作成されています。そのため、Raincluod plots のインプットデータ(PlantHight)の形式を ggplot2 用に直し、作図します。

# データ形式の変更
# 1列目 : WT or Mutant, 2列目 : 草丈 の形式に変わっています
PlantHight_1 <- melt(PlantHight)
head(PlantHight_1)
  variable    value
1       WT 20.81688
2       WT 23.03243
3       WT 23.36832
4       WT 18.66481
5       WT 26.57562
6       WT 15.88725

# 作図
ggplot(PlantHight_1, aes(y = value, x = variable, fill = variable)) + # 作図するデータのインプット、y は数値(草丈)、x は WT or Mutant、fill は図の色の振り分け
  geom_flat_violin(position = position_nudge(x = .2, y = 0), alpha = .5) + # violin plot のポジションと透明度の設定
  geom_boxplot(width = .1,  outlier.shape = NA, alpha = 0.5) + # box plot のポジションと透明度の設定
  geom_point(aes(y = value, color = variable), position = position_jitter(width = .15), size = .8, alpha = 0.8) + # dot plot のポジションと幅、サイズ、透明度の設定
  labs(x="WT or Mutant",y="Plant hight (cm)") + # XY 軸のラベルの名前の設定の設定
theme(legend.position="none") + # レジェンドは書きませんよ
theme(axis.title = element_text(family = "Times New Roman")) + # 軸タイトルのフォントの設定
  theme(axis.text=element_text(size=12, family = "Times New Roman")) + # 軸数値のフォントの設定
  theme(legend.title = element_text(family = "Times New Roman")) + #レジェンドないので実質いらない
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme(panel.background = element_blank()) + # 軸のグリッド線を書きません
  theme(axis.line = element_line(colour = 'black', size = 0.5)) + 
  theme(axis.ticks = element_line(colour = "black", size = 0.4)) #軸の色と太さ
ggsave("~/Desktop/PlantHight.png")
# ggsave("保存したい場所/図の名前.png")

これで作図の完成です。
このような図が出来上がります。
PlantHight.png

色の指定もできますが、力尽きましたのでまた今度よろしくおねがいします。
初めての投稿です。これからたくさんブログについて勉強します。

***すぐさま追記 (2020.03.02) ***
R version 3.5.2, 3.4.0 で行いました。
パッケージのバージョンは以下の通りです(rlibs はインストールできませんでしたが、作図は行えました、原因は追って、、、)

> packageVersion("tidyverse")
[1] 1.2.1
> packageVersion("rmarkdown")
[1] 1.10
> packageVersion("httr")
[1] 1.3.1
> packageVersion("shinydashboard")
[1] 0.7.0
> packageVersion("leaflet")
[1] 2.0.2
> packageVersion("knitr")
[1] 1.20
> packageVersion("ggplot2")
[1] 2.2.1
> packageVersion("dplyr")
[1] 0.7.6
> packageVersion("caTools")
[1] 1.17.1.1
> packageVersion("bitops")
[1] 1.0.6
> packageVersion("reshape2")
[1] 1.4.3

以下参考
R のインストールに関してわかりやすいサイト
RainCloud plots の GitHub page
RainCloud Plots の論文

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?