【问题标题】:Add jitter to ggpaired给ggpaired添加抖动
【发布时间】:2017-12-31 13:20:58
【问题描述】:

ggpubr 包提供函数ggpaired 来绘制配对数据。 它没有 add='jitter' 的选项 ggboxplot 那样。

有没有可能通过不同的方式达到类似的效果?

文档中的示例:

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")

【问题讨论】:

  • 也许function could be edited 提供给position = "jitter"。或者,考虑向 GitHub 存储库添加功能请求。
  • 看起来请求 #407 已于 21 年 4 月 26 日打开。感谢@a-kassambara 考虑添加此功能,感谢您对一款出色的工具的赞誉。

标签: r ggplot2 boxplot ggpubr jitter


【解决方案1】:

以下是一些与ggpaired 生成的情节类似但带有抖动点的情节的想法。

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 2020-11-28
    • 2016-04-03
    • 2016-09-25
    • 2011-12-24
    • 1970-01-01
    相关资源
    最近更新 更多