r - 为 ggpaired 添加抖动

标签 r ggplot2 boxplot ggpubr jitter

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

是否可以通过不同的方式达到类似的效果?

文档中的示例:

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

最佳答案

这里有一些与 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()

enter image description here

关于r - 为 ggpaired 添加抖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48042810/

相关文章:

r - ggplot2:如何将变量的值分配给ggplot标题

R/GIS : Find orthogonal distance between a location and nearest line

r - 使用 Dplyr 将每行列右侧的所有单元格求和

r - 在ggplot2中使用facet_wrap和scales = "free"设置各个轴限制

R:如何将字符列换行以在 ggplot 中打印为 2 行,但它们不是标题?

r - 连接意味着在箱线图上用一条线

python - 使用 Matplotlib 创建箱线图

python - 对于相同的数据,为什么 matplotlib 1.4.0 的 boxplot 与 matplotlib 2.2.0 的 boxplot 不同?

r - 面标签字体大小

r - fatal error : Rcpp. h:没有这样的文件或目录