ggplot의 바이올린 플롯 너비
나는 ggplot을 사용하여 여러 하프-바이올린 플롯 옆에 하나의 전체 바이올린 플롯을 플롯하려고합니다. 저는 geom_violin ()을 사용하여 전체 바이올린 플롯을 플로팅하고 스케일 = 'width'인 "see"패키지의 geom_violinhalf를 사용하여 하프 바이올린 플롯을 플로팅하고 있습니다. 그러나 이것은 아래 코드에서 볼 수 있듯이 전체 바이올린 플롯이 하프-바이올린 플롯보다 훨씬 더 얇게 나타나는 그림을 생성했습니다. ggplot에 내장 된 세 가지 "scale"옵션을 사용해 보았습니다.
하나의 함수를 사용하여 하나의 전체 바이올린 플롯과 여러 개의 하프 바이올린 플롯을 그리는 방법이 있습니까? 아니면 전체 바이올린 플롯의 너비를 수동으로 설정할 수있는 다른 방법이 있습니까?
감사!
#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()
편집하다
이전 코드가 잘못되었습니다. 죄송합니다. 데이터 프레임이 다음과 같고 각 바이올린 플롯이 'x'대신 'd'로 그룹화 된 경우 첫 번째 바이올린을 어떻게 확장 할 수 있습니까?
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숫자 를 유지 하고의 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()