在ggplot2(R)中添加箱线图和统计数据以分割小提琴图

qgelzfjb  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(85)

我喜欢小提琴的分裂情节和@ jan-glx在这里创建的令人敬畏的geom_split_violin函数:Split violin plot with ggplot2
我很乐意添加分裂箱线图和统计数据,如下所述。
首先,为了完整,这里是完整的数据和代码。

数据(复制自上面链接)

set.seed(20160229)
 my_data = data.frame(
     y=c(rnorm(1000), rnorm(1000, 0.5), rnorm(1000, 1), rnorm(1000, 1.5)),
     x=c(rep('a', 2000), rep('b', 2000)),
     m=c(rep('i', 1000), rep('j', 2000), rep('i', 1000)))

创建geom_split_violin函数的代码(复制自上面链接)

library('ggplot2')
 GeomSplitViolin <- ggproto("GeomSplitViolin", GeomViolin, 
                       draw_group = function(self, data, ..., draw_quantiles = NULL) {
    data <- transform(data, xminv = x - violinwidth * (x - xmin), xmaxv = x + violinwidth * (xmax - x))
   grp <- data[1, "group"]
   newdata <- plyr::arrange(transform(data, x = if (grp %% 2 == 1) xminv else xmaxv), if (grp %% 2 == 1) y else -y)
   newdata <- rbind(newdata[1, ], newdata, newdata[nrow(newdata), ], newdata[1, ])
   newdata[c(1, nrow(newdata) - 1, nrow(newdata)), "x"] <- round(newdata[1, "x"])
   if (length(draw_quantiles) > 0 & !scales::zero_range(range(data$y))) {
     stopifnot(all(draw_quantiles >= 0), all(draw_quantiles <=
       1))
     quantiles <- ggplot2:::create_quantile_segment_frame(data, draw_quantiles)
     aesthetics <- data[rep(1, nrow(quantiles)), setdiff(names(data), c("x", "y")), drop = FALSE]
     aesthetics$alpha <- rep(1, nrow(quantiles))
     both <- cbind(quantiles, aesthetics)
     quantile_grob <- GeomPath$draw_panel(both, ...)
     ggplot2:::ggname("geom_split_violin", grid::grobTree(GeomPolygon$draw_panel(newdata, ...), quantile_grob))
   }
   else {
     ggplot2:::ggname("geom_split_violin", GeomPolygon$draw_panel(newdata, ...))
   }
 })
 geom_split_violin <- function(mapping = NULL, data = NULL, stat = "ydensity", position = "identity", ..., 
                               draw_quantiles = NULL, trim = TRUE, scale = "area", na.rm = FALSE, 
                               show.legend = NA, inherit.aes = TRUE) {
   layer(data = data, mapping = mapping, stat = stat, geom = GeomSplitViolin, 
         position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
         params = list(trim = trim, scale = scale, draw_quantiles = draw_quantiles, na.rm = na.rm, ...))
 }

我尝试添加箱线图和统计数据

下面是我用来尝试添加的代码:
1.分割箱线图。
1.使用wilcox.test统计数据的P值。
1.样本量(n)。
代码:

library(ggpubr)
 give.n <- function(x){return(y = -2.6, label = length(x))}
 ggplot(my_data, aes(x, y, fill = m)) + 
      geom_split_violin() + 
      geom_boxplot(width = 0.2, notch = TRUE, fill="white", outlier.shape = NA) + 
      stat_summary(fun.data = give.n, geom = "text") + 
      stat_compare_means(aes(label = ifelse(p < 1.e-4, sprintf("p = %2.1e", 
           as.numeric(..p.format..)), sprintf("p = %5.4f", 
           as.numeric(..p.format..)))), method = "wilcox.test", paired = FALSE) + 
      stat_summary(fun.data = give.n, geom = "text")

结果如下:x1c 0d1x不幸的是,这抛出了一个错误,并且不完全是我希望得到的,因为它缺少p值和样本大小(n),并且箱线图没有分裂。我也在另一个SO答案中尝试了@Axeman的一个极好的建议,但到目前为止还没有运气。
我希望实现的是与此类似的东西(p值不再是“NA”):

这似乎是一个很大的挑战,但我希望有人在那里可能能够帮助,因为其他人可能会喜欢这个和我一样多。谢谢

eagi6jfj

eagi6jfj1#

看起来这个主题可能已经解决了这个问题(使用与您相同的源代码):ggplot split violin plot with horizontal mean lines
以下是一些示例数据:

set.seed(20160229)

my_data = data.frame(
  y=c(rnorm(1000), rnorm(1000, 0.5), rnorm(1000, 1), rnorm(1000, 
1.5)),
  x=c(rep('a', 2000), rep('b', 2000)),
  m=c(rep('i', 1000), rep('j', 2000), rep('i', 1000))
)

下面是geom_violin创建split_geom_violin的扩展:

GeomSplitViolin <- ggproto("GeomSplitViolin", GeomViolin, draw_group = function(self, data, ..., draw_quantiles = NULL){
  data <- transform(data, xminv = x - violinwidth * (x - xmin), xmaxv = x + violinwidth * (xmax - x))
  grp <- data[1,'group']
  newdata <- plyr::arrange(transform(data, x = if(grp%%2==1) xminv else xmaxv), if(grp%%2==1) y else -y)
  newdata <- rbind(newdata[1, ], newdata, newdata[nrow(newdata), ], newdata[1, ])
  newdata[c(1,nrow(newdata)-1,nrow(newdata)), 'x'] <- round(newdata[1, 'x']) 
  if (length(draw_quantiles) > 0 & !scales::zero_range(range(data$y))) {
    stopifnot(all(draw_quantiles >= 0), all(draw_quantiles <= 
                                              1))
    quantiles <- ggplot2:::create_quantile_segment_frame(data, draw_quantiles)
    aesthetics <- data[rep(1, nrow(quantiles)), setdiff(names(data), c("x", "y")), drop = FALSE]
    aesthetics$alpha <- rep(1, nrow(quantiles))
    both <- cbind(quantiles, aesthetics)
    quantile_grob <- GeomPath$draw_panel(both, ...)
    ggplot2:::ggname("geom_split_violin", grid::grobTree(GeomPolygon$draw_panel(newdata, ...), quantile_grob))
  }
  else {
    ggplot2:::ggname("geom_split_violin", GeomPolygon$draw_panel(newdata, ...))
  }
})

geom_split_violin <- function (mapping = NULL, data = NULL, stat = "ydensity", position = "identity", ..., draw_quantiles = NULL, trim = TRUE, scale = "area", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) {
  layer(data = data, mapping = mapping, stat = stat, geom = GeomSplitViolin, position = position, show.legend = show.legend, inherit.aes = inherit.aes, params = list(trim = trim, scale = scale, draw_quantiles = draw_quantiles, na.rm = na.rm, ...))
}

下面是图的代码:

library(ggplot2)
ggplot(my_data, aes(x, y, fill=m)) + 
  geom_split_violin(trim = TRUE) + 
  geom_boxplot(width = 0.25, notch = FALSE, notchwidth = .4, outlier.shape = NA, coef=0) +
  labs(x=NULL,y="GM Attitude Score") +
  theme_classic() +
  theme(text = element_text(size = 20)) +
  scale_x_discrete(labels=c("0" = "Control\nCondition", "1" = "GM\nCondition")) +
  scale_fill_manual(values=c("#E69F00", "#999999"), 
                    name="Survey\nPart",
                    breaks=c("1", "2"),
                    labels=c("Time 1", "Time 5"))

Produced graph

相关问题