r - 在 R 中将样本与 fiddle 图配对

标签 r ggplot2 violin-plot ggpubr

我正在尝试从 ggpaired (ggpubr) 重现此图,但使用 fiddle 图而不是箱线图:

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",
     fill = "condition", palette = "jco")

我尝试过使用 ggplot2 中的 geom_violin,但没有成功地像 ggpaired 那样配对样本

paired boxplot

最佳答案

您需要首先 reshape 数据:

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)

d <- data.frame(before = before, after = after)
d$obs <- 1:nrow(d)
d2 <- tidyr::gather(d, time, value, -obs)

ggplot(d2, aes(time, value)) + 
  geom_violin() +
  geom_point() +
  geom_line(aes(group = obs)) +
  scale_x_discrete(limits = c('before', 'after'))

enter image description here

关于r - 在 R 中将样本与 fiddle 图配对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58122893/

相关文章:

r - 如何平滑密度曲线以减少峰值?

删除 R 中特殊字符的引号

r - 在函数内使用 ggplot 创建的绘图出现多图错误

python - matplotlib/seaborn 带颜色图的 fiddle 图

r - 对 R 中单列的重叠行求和

r - 将两个 GRanges 对象相减

r - R中ggplot2 barplot中的可变宽度条

r - 在 ggplot 中制作多个几何图形动画

python - 更改 matplotlib fiddle 图的颜色

r - 用 geom_violin 填充透明度