繁体   English   中英

ggplot中的小提琴图宽度

[英]Violin Plot Width in ggplot

我正在尝试使用 ggplot 在多个半小提琴图旁边绘制一个完整的小提琴图。 我正在使用 geom_violin() 绘制完整的小提琴图,并使用“see”包中的 geom_violinhalf 绘制半小提琴图, scale = 'width'。 然而,这产生了一个图形,其中完整的小提琴图看起来比半小提琴图明显更薄,如下面的代码所示。 我已经在 ggplot 中尝试了三个内置的“缩放”选项。

有没有办法使用一个函数绘制一个完整的小提琴图和几个半小提琴图? 或者有没有其他方法可以手动设置完整小提琴图的宽度?

谢谢!

#install.packages("vioplot")
#install.packages('ggplot2')
#install.packages('scales')
library(ggplot2)
library(vioplot)
library(scales)

df1 = as.data.frame(matrix(NA, nrow = 1000, ncol = 2))
colnames(df1) = c('x','y')
df1[,2] = rnorm(1000,10,5)
df1[,1] = rep(-5, 1000)
df2 = as.data.frame(matrix(NA, nrow = 10000, ncol = 2))
colnames(df2) = c('x','y')
df2[,1] = sample(seq(0,70,5),10000,replace=TRUE)
df2[,2] = rnorm(10000,10,5)
df = rbind(df1, df2)

ggplot(df, aes(x=x,y=y))+
  geom_violinhalf(data=subset(df,df$x != -5), aes(x=x, group = x), scale = 'width', fill = alpha('red', 0.2), color = alpha('red',0.2))+
  geom_violin(data = subset(df, df$x== -5), color = alpha('red', 0.2), fill = alpha('red', 0.2))+
  geom_point(size = 1.2, col = alpha('grey', 0.2))+
  theme_classic()

编辑

之前的代码不正确,抱歉。 如果数据框如下所示,第一把小提琴如何扩展,每个小提琴图按“d”而不是“x”分组:

df1 = as.data.frame(matrix(NA, nrow = 10000, ncol = 3))
colnames(df1) = c('d','y','x')
df1[,2] = rnorm(10000,10,5)
df1[,1] = sample(seq(-5,70,.1),10000, replace=TRUE)
for(i in 1:nrow(df1)){
  if(df1[i,'d'] < 0){df1[i,"x"] <- -5}else
    if(df1[i,"d"] >= 0 & df1[i,"d"] < 5){df1[i,"x"] <- 0}else
      if(df1[i,"d"] >= 5 & df1[i,"d"] < 10){df1[i,"x"] <- 5}else
        if(df1[i,"d"] >= 10 & df1[i,"d"] < 15){df1[i,"x"] <- 10}else
          if(df1[i,"d"] >=15 & df1[i,"d"] < 20){df1[i,"x"] <- 15}else
            if(df1[i,"d"] >= 20 & df1[i,"d"] < 25){df1[i,"x"] <- 20}else
              if(df1[i,"d"] >= 25 & df1[i,"d"] < 30){df1[i,"x"] <- 25}else
                if(df1[i,"d"] >= 30 & df1[i,"d"] < 35){df1[i,"x"] <- 30}else
                  if(df1[i,"d"] >= 35 & df1[i,"d"] < 40){df1[i,"x"] <- 35}else
                    if(df1[i,"d"] >= 40 & df1[i,"d"] < 45){df1[i,"x"] <- 40}else
                      if(df1[i,"d"] >= 45 & df1[i,"d"] < 50){df1[i,"x"] <- 45}else
                        if(df1[i,"d"] >= 50 & df1[i,"d"] < 55){df1[i,"x"] <- 50}else
                          if(df1[i,"d"] >= 55 & df1[i,"d"] < 60){df1[i,"x"] <- 55}else
                            if(df1[i,"d"] >= 60 & df1[i,"d"] < 65){df1[i,"x"] <- 60}else
                              if(df1[i,"d"] >= 65 & df1[i,"d"] < 70){df1[i,"x"] <- 65}else
                                if(df1[i,"d"] >= 70 & df1[i,"d"] < 75){df1[i,"x"] <- 70}else
                                  if(df1[i,"d"] >= 75 & df1[i,"d"] < 80){df1[i,"x"] <- 75}}

如果将 x 转换为因子,则默认情况下宽度将相等:

ggplot(df, aes(x=factor(x),y=y)) +
  geom_violinhalf(data=subset(df,df$x != -5), aes(group = x), 
                  scale = 'width', fill = alpha('red', 0.2), 
                  color = alpha('red',0.2)) +
  geom_violin(data = subset(df, df$x== -5), 
              color = alpha('red', 0.2), fill = alpha('red', 0.2)) +
  geom_point(size = 1.2, col = alpha('grey', 0.2)) +
  theme_classic()

在此处输入图片说明

或者,您可以保留x数字并使用 geom_violin 的width参数geom_violin 在这里,为了演示目的,我将它设置为width = 8有点太宽,但理想情况似乎是width = 5

ggplot(df, aes(x = x, y = y)) +
  geom_violinhalf(data = subset(df,df$x != -5), aes(group = x), 
                  scale = 'width', fill = alpha('red', 0.2), 
                  color = alpha('red',0.2)) +
  geom_violin(data = subset(df, df$x == -5), width = 8,
              color = alpha('red', 0.2), fill = alpha('red', 0.2)) +
  geom_point(size = 1.2, col = alpha('grey', 0.2)) +
  theme_classic()

在此处输入图片说明


编辑

要添加 d 列中的点,我们可以执行以下操作:

ggplot(df1, aes(x=x,y=y)) +
  geom_violinhalf(data=subset(df1,df1$x != -5), aes(group = x), 
                  scale = 'width', fill = alpha('red', 0.2), 
                  color = alpha('red',0.2)) +
  geom_violin(data = subset(df1, df1$x== -5), width = 5, 
              color = alpha('red', 0.2), fill = alpha('red', 0.2)) +
  geom_point(aes(x = d), size = 1.2, col = alpha('grey', 0.2)) +
  theme_classic()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM