从 {ggdist} 包中旋转一半小提琴。

huangapple go评论62阅读模式
英文:

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
  )

结果如下:

从 {ggdist} 包中旋转一半小提琴。

如何将左侧的半小提琴图旋转到相应箱线图的左侧?我尝试过使用 justification = c(0.2, -0.2),但会报错。

英文:

For this data:

set.seed(123)
df &lt;- 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 %&gt;%
  pivot_longer(-ID) %&gt;% 
  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
  )

The result is this:
从 {ggdist} 包中旋转一半小提琴。

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

使用 ifelsejustification 以及 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
  )

从 {ggdist} 包中旋转一半小提琴。

英文:

Using an ifelse and the justification and side aesthetics you could do:

set.seed(123)
df &lt;- data.frame(
  ID = 1:50,
  Q = rnorm(50),
  A = rnorm(50, 1)
)

library(tidyverse)
library(ggdist)

df %&gt;%
  pivot_longer(-ID) %&gt;%
  ggplot(aes(x = factor(name), y = value, fill = factor(name))) +
  stat_halfeye(
    aes(
      justification = ifelse(name == &quot;A&quot;, 1.2, -.2),
      side = ifelse(name == &quot;A&quot;, &quot;left&quot;, &quot;right&quot;)
    ),
    adjust = 0.5,
    .width = 0,
    point_colour = NA
  ) +
  geom_boxplot(
    width = 0.12,
    outlier.color = NA,
    alpha = 0.5
  )

从 {ggdist} 包中旋转一半小提琴。<!-- -->

huangapple
  • 本文由 发表于 2023年4月4日 15:56:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定