分开盒图和geom_path的位置。

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

Separate positions of boxplots and geom_path

问题

以下是代码的翻译部分:

对于这个示例数据:

    set.seed(123)
    df <- data.frame(
      ID = 1:50,
      Q = rnorm(50),
      A = rnorm(50,1)
    )
    
我想要连接`ID`上的成对点,可以借助以下答案实现:https://stackoverflow.com/questions/75927315/connect-jittered-points-by-group:

    pj <- position_jitter(seed = 1, width = .1, height = 0)
    
    df %>%
      pivot_longer(-ID) %>%
      ggplot(aes(x = factor(name), y = value, fill = factor(name))) +
    
      # 箱线图:
      geom_boxplot(
        width = 0.12,
        outlier.color = NA,
        alpha = 0.5
      ) +
      
      # 数据点:
      geom_point(
        alpha = 0.5, col = "blue",
        position = pj
      ) +
      
      # 连接线:
      geom_path(aes(group = ID),
                alpha = 0.5,
                position = pj
      )

希望这对你有帮助。

英文:

For this illustrative data:

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

I want to connect the paired points across ID, which is possible with the help of this answer https://stackoverflow.com/questions/75927315/connect-jittered-points-by-group:

pj &lt;- position_jitter(seed = 1, width = .1, height = 0)

df %&gt;%
  pivot_longer(-ID) %&gt;%
  ggplot(aes(x = factor(name), y = value, fill = factor(name))) +

  # boxplot:
  geom_boxplot(
    width = 0.12,
    outlier.color = NA,
    alpha = 0.5
  ) +
  
  # data points:
  geom_point(
    alpha = 0.5, col = &quot;blue&quot;,
    position = pj
  ) +
  
  # connecting lines:
  geom_path(aes(group = ID),
            alpha = 0.5,
            position = pj
  )

分开盒图和geom_path的位置。

What bothers me is that the points overplot the boxplots. I would like them to be separated from the boxplots. Specifically they should be moved inside the space between the boxes, like in this plot:

分开盒图和geom_path的位置。

How can this be achieved? Many thanks in advance.

答案1

得分: 2

以下是代码的翻译部分:

一种选择是移动或微调点和线的位置,需要将 name 转换为数字,并考虑箱线图和抖动宽度:

library(tidyr)
library(dplyr, warn = FALSE)
library(ggplot2)

box_width <- .12
jitter_width <- .1

pj <- position_jitter(seed = 1, width = jitter_width, height = 0)

df %>%
  pivot_longer(-ID) %>%
  mutate(
    name_num = as.numeric(factor(name)),
    name_num = name_num + (box_width + jitter_width / 2) * if_else(name == "A", 1, -1)
  ) %>%
  ggplot(aes(x = factor(name), y = value, fill = factor(name))) +
  geom_boxplot(
    width = box_width,
    outlier.color = NA,
    alpha = 0.5
  ) +
  geom_point(
    aes(x = name_num),
    alpha = 0.5, col = "blue",
    position = pj
  ) +
  geom_path(aes(x = name_num, group = ID),
    alpha = 0.5,
    position = pj
  )

编辑 要交换 name 类别的位置,请在转换为 factor 时设置所需顺序的级别,并在 ifelse 中考虑这一点。

library(tidyr)
library(dplyr, warn = FALSE)
library(ggplot2)

box_width <- .12
jitter_width <- .1

pj <- position_jitter(seed = 1, width = jitter_width, height = 0)

df %>%
  pivot_longer(-ID) %>%
  mutate(
    name = factor(name, levels = c("Q", "A")),
    name_num = as.numeric(factor(name)),
    name_num = name_num + (box_width + jitter_width / 2) * if_else(name == "Q", 1, -1)
  ) %>%
  ggplot(aes(x = name, y = value, fill = name)) +
  geom_boxplot(
    width = box_width,
    outlier.color = NA,
    alpha = 0.5
  ) +
  geom_point(
    aes(x = name_num),
    alpha = 0.5, col = "blue",
    position = pj
  ) +
  geom_path(aes(x = name_num, group = ID),
    alpha = 0.5,
    position = pj
  )

希望这有所帮助。

英文:

One option would be to shift or nudge the positions of the points and the lines which requires to convert name to a numeric and to take account of the boxplot and the jitter width:

library(tidyr)
library(dplyr, warn = FALSE)
library(ggplot2)

box_width &lt;- .12
jitter_width &lt;- .1

pj &lt;- position_jitter(seed = 1, width = jitter_width, height = 0)

df %&gt;%
  pivot_longer(-ID) %&gt;%
  mutate(
    name_num = as.numeric(factor(name)),
    name_num = name_num + (box_width + jitter_width / 2) * if_else(name == &quot;A&quot;, 1, -1)
  ) |&gt;
  ggplot(aes(x = factor(name), y = value, fill = factor(name))) +
  geom_boxplot(
    width = box_width,
    outlier.color = NA,
    alpha = 0.5
  ) +
  geom_point(
    aes(x = name_num),
    alpha = 0.5, col = &quot;blue&quot;,
    position = pj
  ) +
  geom_path(aes(x = name_num, group = ID),
    alpha = 0.5,
    position = pj
  )

分开盒图和geom_path的位置。

EDIT To switch the positions of the name categories set the levels in your desired order when converting to a factor. And of course do we have to take account of that in the ifelse.

library(tidyr)
library(dplyr, warn = FALSE)
library(ggplot2)

box_width &lt;- .12
jitter_width &lt;- .1

pj &lt;- position_jitter(seed = 1, width = jitter_width, height = 0)

df %&gt;%
  pivot_longer(-ID) %&gt;%
  mutate(
    name = factor(name, levels = c(&quot;Q&quot;, &quot;A&quot;)),
    name_num = as.numeric(factor(name)),
    name_num = name_num + (box_width + jitter_width / 2) * if_else(name == &quot;Q&quot;, 1, -1)
  ) |&gt;
  ggplot(aes(x = name, y = value, fill = name)) +
  geom_boxplot(
    width = box_width,
    outlier.color = NA,
    alpha = 0.5
  ) +
  geom_point(
    aes(x = name_num),
    alpha = 0.5, col = &quot;blue&quot;,
    position = pj
  ) +
  geom_path(aes(x = name_num, group = ID),
    alpha = 0.5,
    position = pj
  )

分开盒图和geom_path的位置。

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

发表评论

匿名网友

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

确定