Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
由于缺少值,ggplot2(R)中拆分小提琴图中的标签出错_R_Ggplot2_Label_Tidyverse_Violin Plot - Fatal编程技术网

由于缺少值,ggplot2(R)中拆分小提琴图中的标签出错

由于缺少值,ggplot2(R)中拆分小提琴图中的标签出错,r,ggplot2,label,tidyverse,violin-plot,R,Ggplot2,Label,Tidyverse,Violin Plot,我正在使用此处创建的geom_split_violen函数生成分割小提琴图: 然后,我为每个拆分的小提琴添加样本大小标签(n=…)。但是,存在一些缺失值,这会导致从缺失数据开始的错误标签 使用下面的代码,结果如下: 在底部网格(B)中,p2下没有填充值value=1。这会导致此后分裂小提琴的标签错误。具体而言,标签n=3和n=108被交换 有没有办法解决这个问题 以下是完整的代码: # Create data set.seed(12345) my_data = data.frame(

我正在使用此处创建的
geom_split_violen
函数生成分割小提琴图:

然后,我为每个拆分的小提琴添加样本大小标签(n=…)。但是,存在一些缺失值,这会导致从缺失数据开始的错误标签

使用下面的代码,结果如下:

在底部网格(B)中,p2下没有填充值
value=1
。这会导致此后分裂小提琴的标签错误。具体而言,标签
n=3
n=108
被交换

有没有办法解决这个问题

以下是完整的代码:

 # Create data
 set.seed(12345)
 my_data = data.frame(
     y=c(rnorm(1000, 15)),
     type=c(rep("A", 635), rep("B", 365)),
     variable=c(rep("p1", 151), rep("p2", 287), rep("p3", 197), 
             rep("p1", 73), rep("p2", 181), rep("p3", 111)),
     value=c(rep("0", 89), rep("1", 62), 
            rep("0", 151), rep("1", 136), 
            rep("0", 101), rep("1", 96), 
            rep("0", 39), rep("1", 34), 
            rep("0", 181), 
            rep("0", 108), rep("1", 3)))
 # Code to create geom_split_violin function from link above
 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, ...))
 }
 # Add labels 'n = ...'
 give_n = function(x, y_lo = 12) {
      data.frame(y = y_lo,
              label = paste("n =", length(x)))
 }
 # Plot data
 ggplot(my_data, aes(variable, y, fill = value)) + 
      geom_split_violin() + 
      facet_grid(type ~ ., scales = "free_y") + 
      stat_summary(fun.data = give_n, aes(x = as.factor(variable)), 
      geom = "text", position = position_nudge(x = c(-0.25, 0.25)))
#创建数据
种子集(12345)
my_data=data.frame(
y=c(rnorm(1000,15)),
类型=c(代表(“A”,635),代表(“B”,365)),
变量=c(代表(“p1”,151),代表(“p2”,287),代表(“p3”,197),
代表(“p1”,73),代表(“p2”,181),代表(“p3”,111)),
值=c(代表(“0”,89),代表(“1”,62),
代表(“0”,151),代表(“1”,136),
代表(“0”,101),代表(“1”,96),
代表(“0”,39),代表(“1”,34),
代表(“0”,181),
代表(“0”,108),代表(“1”,3)))
#从上面的链接创建geom_split_小提琴函数的代码
库('ggplot2')

对于b/p2,您有一个长度为1的标签,但有一个长度为2的微移(
x=c(-0.25,0.25)
),因此它会忽略其余部分。您可以尝试
位置\u道奇(宽度=1)
而不是前进!好极了非常感谢你!如果你想回答这个问题,我会打勾并投票表决。