#Install the package
install.packages("ggplot2") #パッケージのインストール
library(ggplot2) #パッケージの呼び出し
#Preparation of dataset
sample(CO2) #サンプルデータセット読み込み
attach(CO2)
Type <- as.factor(Type) #Typeを因子型に変換する
#Create violin plot
ggplot(CO2, aes(x=Type, y=uptake), fill=Type)) +
geom_violin() +
theme_bw() #バイオリンプロット作成
回帰モデルの残差の分布を確認することもできる。
lm1 <- lm(uptake ~ Type, data=CO2) #回帰モデル作成
#Create violin plot
ggplot(CO2, aes(x=Type, y=resid(lm1), fill=Type)) +
geom_violin() +
theme_bw() #バイオリンプロット作成