2017-12-31 234 views
0

ggpubrパッケージには、ペアデータをプロットするための関数ggpairedが用意されています。 オプションadd='jitter'ggboxplotとはありません。ggpairedにジッタを追加

異なる手段で同様の効果を得ることはできますか?ドキュメントから

例:ここでは

require(ggplot) 
require(ggpubr) 
before <-c(200.1, 190.9, 192.7, 213, 241.4, 196.9, 172.2, 185.5, 205.2, 193.7) 
after <-c(392.9, 393.2, 345.1, 393, 434, 427.9, 422, 383.9, 392.3, 352.2) 
d <- data.frame(before = before, after = after) 
ggpaired(d, cond1 = "before", cond2 = "after", add="jitter") 


data("ToothGrowth") 
df <- ToothGrowth 
ggboxplot(df, x = "dose", y = "len", width = 0.8, add="jitter") 
+1

"ジッタ"。または、GitHubリポジトリに機能要求を追加することを検討してください。 –

答えて

3

ggpairedによってではなく、ジッタの点で生成されたものと同様のプロットのためのいくつかのアイデアです。おそらく=位置 `を提供する(https://github.com/kassambara/ggpubr/blob/master/R/ggpaired.R#L124)関数を編集することができる]

library(ggplot2) 
before <- c(200.1, 190.9, 192.7, 213, 241.4, 196.9, 172.2, 185.5, 205.2, 193.7) 
after <- c(392.9, 393.2, 345.1, 393, 434, 427.9, 422, 383.9, 392.3, 352.2) 
n <- length(before) 
d <- data.frame(y = c(before, after), 
       x = rep(c(1,2), each=n), 
       id = factor(rep(1:n,2))) 

set.seed(321)  
d$xj <- jitter(d$x, amount=.03) 
ggplot(data=d, aes(y=y)) + 
    geom_boxplot(aes(x=x, group=x), width=0.2, outlier.shape = NA) + 
    geom_point(aes(x=xj)) + 
    geom_line(aes(x=xj, group=id)) + 
    xlab("Condition") + ylab("Value") + 
    scale_x_continuous(breaks=c(1,2), labels=c("Before", "After"), limits=c(0.5, 2.5)) + 
    theme_bw() 

enter image description here

関連する問題