英文:
Rotate half-violin from {ggdist} package
问题
对于这段代码:
set.seed(123)
df <- data.frame(
ID = 1:50,
Q = rnorm(50),
A = rnorm(50,1)
)
我正在绘制一个带有半小提琴图的雨云图,使用了 ggdist
包:
library(tidyverse)
library(ggdist)
df %>%
pivot_longer(-ID) %>%
ggplot(aes(x = factor(name), y = value, fill = factor(name)))+
# 添加半小提琴图
stat_halfeye(
# 调整带宽
adjust = 0.5,
# 向右移动
justification = -0.2,
# 移除杂质间隔
.width = 0,
point_colour = NA
)+
geom_boxplot(
width = 0.12,
# 移除离群值
outlier.color = NA,
alpha = 0.5
)
结果如下:
如何将左侧的半小提琴图旋转到相应箱线图的左侧?我尝试过使用 justification = c(0.2, -0.2)
,但会报错。
英文:
For this data:
set.seed(123)
df <- data.frame(
ID = 1:50,
Q = rnorm(50),
A = rnorm(50,1)
)
I'm drawing a raincloud plot with half-violins from the ggdist
package:
library(tidyverse)
library(ggdist)
df %>%
pivot_longer(-ID) %>%
ggplot(aes(x = factor(name), y = value, fill = factor(name)))+
# add half-violin
stat_halfeye(
# adjust bandwidth
adjust = 0.5,
# move to the right
justification = -0.2,
# remove the slub interval
.width = 0,
point_colour = NA
)+
geom_boxplot(
width = 0.12,
# removing outliers
outlier.color = NA,
alpha = 0.5
)
How can I rotate the left half-violin to the left side of the respective boxplot? I've tried to use justification = c(0.2, -0.2)
but that throws an error.
答案1
得分: 1
使用 ifelse
和 justification
以及 side
美学,您可以执行以下操作:
set.seed(123)
df <- data.frame(
ID = 1:50,
Q = rnorm(50),
A = rnorm(50, 1)
)
library(tidyverse)
library(ggdist)
df %>%
pivot_longer(-ID) %>%
ggplot(aes(x = factor(name), y = value, fill = factor(name))) +
stat_halfeye(
aes(
justification = ifelse(name == "A", 1.2, -.2),
side = ifelse(name == "A", "left", "right")
),
adjust = 0.5,
.width = 0,
point_colour = NA
) +
geom_boxplot(
width = 0.12,
outlier.color = NA,
alpha = 0.5
)
英文:
Using an ifelse
and the justification
and side
aesthetics you could do:
set.seed(123)
df <- data.frame(
ID = 1:50,
Q = rnorm(50),
A = rnorm(50, 1)
)
library(tidyverse)
library(ggdist)
df %>%
pivot_longer(-ID) %>%
ggplot(aes(x = factor(name), y = value, fill = factor(name))) +
stat_halfeye(
aes(
justification = ifelse(name == "A", 1.2, -.2),
side = ifelse(name == "A", "left", "right")
),
adjust = 0.5,
.width = 0,
point_colour = NA
) +
geom_boxplot(
width = 0.12,
outlier.color = NA,
alpha = 0.5
)
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论