我正在尝试使用ggplot将一个完整的小提琴图绘制在多个半小提琴图旁边。我正在使用geom_violin()绘制完整的小提琴图,并使用来自“ see”包中的geom_violinhalf绘制半小提琴图,比例='宽度'。但是,这产生了一个完整的小提琴图看上去比半小提琴图显着细的图形,如以下代码所示。我已经尝试了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}}
答案 0 :(得分:3)
如果将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
的数字并使用width
的{{1}}参数。在这里,出于演示目的,我将其设置在geom_violin
处有点太宽,但是理想情况似乎是在width = 8
左右:
width = 5
编辑
要添加d列中的点,我们可以这样做:
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()