分开盒图和geom_path的位置。

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

Separate positions of boxplots and geom_path

问题

以下是代码的翻译部分:

  1. 对于这个示例数据:
  2. set.seed(123)
  3. df <- data.frame(
  4. ID = 1:50,
  5. Q = rnorm(50),
  6. A = rnorm(50,1)
  7. )
  8. 我想要连接`ID`上的成对点,可以借助以下答案实现:https://stackoverflow.com/questions/75927315/connect-jittered-points-by-group:
  9. pj <- position_jitter(seed = 1, width = .1, height = 0)
  10. df %>%
  11. pivot_longer(-ID) %>%
  12. ggplot(aes(x = factor(name), y = value, fill = factor(name))) +
  13. # 箱线图:
  14. geom_boxplot(
  15. width = 0.12,
  16. outlier.color = NA,
  17. alpha = 0.5
  18. ) +
  19. # 数据点:
  20. geom_point(
  21. alpha = 0.5, col = "blue",
  22. position = pj
  23. ) +
  24. # 连接线:
  25. geom_path(aes(group = ID),
  26. alpha = 0.5,
  27. position = pj
  28. )

希望这对你有帮助。

英文:

For this illustrative data:

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

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:

  1. pj &lt;- position_jitter(seed = 1, width = .1, height = 0)
  2. df %&gt;%
  3. pivot_longer(-ID) %&gt;%
  4. ggplot(aes(x = factor(name), y = value, fill = factor(name))) +
  5. # boxplot:
  6. geom_boxplot(
  7. width = 0.12,
  8. outlier.color = NA,
  9. alpha = 0.5
  10. ) +
  11. # data points:
  12. geom_point(
  13. alpha = 0.5, col = &quot;blue&quot;,
  14. position = pj
  15. ) +
  16. # connecting lines:
  17. geom_path(aes(group = ID),
  18. alpha = 0.5,
  19. position = pj
  20. )

分开盒图和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 转换为数字,并考虑箱线图和抖动宽度:

  1. library(tidyr)
  2. library(dplyr, warn = FALSE)
  3. library(ggplot2)
  4. box_width <- .12
  5. jitter_width <- .1
  6. pj <- position_jitter(seed = 1, width = jitter_width, height = 0)
  7. df %>%
  8. pivot_longer(-ID) %>%
  9. mutate(
  10. name_num = as.numeric(factor(name)),
  11. name_num = name_num + (box_width + jitter_width / 2) * if_else(name == "A", 1, -1)
  12. ) %>%
  13. ggplot(aes(x = factor(name), y = value, fill = factor(name))) +
  14. geom_boxplot(
  15. width = box_width,
  16. outlier.color = NA,
  17. alpha = 0.5
  18. ) +
  19. geom_point(
  20. aes(x = name_num),
  21. alpha = 0.5, col = "blue",
  22. position = pj
  23. ) +
  24. geom_path(aes(x = name_num, group = ID),
  25. alpha = 0.5,
  26. position = pj
  27. )

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

  1. library(tidyr)
  2. library(dplyr, warn = FALSE)
  3. library(ggplot2)
  4. box_width <- .12
  5. jitter_width <- .1
  6. pj <- position_jitter(seed = 1, width = jitter_width, height = 0)
  7. df %>%
  8. pivot_longer(-ID) %>%
  9. mutate(
  10. name = factor(name, levels = c("Q", "A")),
  11. name_num = as.numeric(factor(name)),
  12. name_num = name_num + (box_width + jitter_width / 2) * if_else(name == "Q", 1, -1)
  13. ) %>%
  14. ggplot(aes(x = name, y = value, fill = name)) +
  15. geom_boxplot(
  16. width = box_width,
  17. outlier.color = NA,
  18. alpha = 0.5
  19. ) +
  20. geom_point(
  21. aes(x = name_num),
  22. alpha = 0.5, col = "blue",
  23. position = pj
  24. ) +
  25. geom_path(aes(x = name_num, group = ID),
  26. alpha = 0.5,
  27. position = pj
  28. )

希望这有所帮助。

英文:

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:

  1. library(tidyr)
  2. library(dplyr, warn = FALSE)
  3. library(ggplot2)
  4. box_width &lt;- .12
  5. jitter_width &lt;- .1
  6. pj &lt;- position_jitter(seed = 1, width = jitter_width, height = 0)
  7. df %&gt;%
  8. pivot_longer(-ID) %&gt;%
  9. mutate(
  10. name_num = as.numeric(factor(name)),
  11. name_num = name_num + (box_width + jitter_width / 2) * if_else(name == &quot;A&quot;, 1, -1)
  12. ) |&gt;
  13. ggplot(aes(x = factor(name), y = value, fill = factor(name))) +
  14. geom_boxplot(
  15. width = box_width,
  16. outlier.color = NA,
  17. alpha = 0.5
  18. ) +
  19. geom_point(
  20. aes(x = name_num),
  21. alpha = 0.5, col = &quot;blue&quot;,
  22. position = pj
  23. ) +
  24. geom_path(aes(x = name_num, group = ID),
  25. alpha = 0.5,
  26. position = pj
  27. )

分开盒图和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.

  1. library(tidyr)
  2. library(dplyr, warn = FALSE)
  3. library(ggplot2)
  4. box_width &lt;- .12
  5. jitter_width &lt;- .1
  6. pj &lt;- position_jitter(seed = 1, width = jitter_width, height = 0)
  7. df %&gt;%
  8. pivot_longer(-ID) %&gt;%
  9. mutate(
  10. name = factor(name, levels = c(&quot;Q&quot;, &quot;A&quot;)),
  11. name_num = as.numeric(factor(name)),
  12. name_num = name_num + (box_width + jitter_width / 2) * if_else(name == &quot;Q&quot;, 1, -1)
  13. ) |&gt;
  14. ggplot(aes(x = name, y = value, fill = name)) +
  15. geom_boxplot(
  16. width = box_width,
  17. outlier.color = NA,
  18. alpha = 0.5
  19. ) +
  20. geom_point(
  21. aes(x = name_num),
  22. alpha = 0.5, col = &quot;blue&quot;,
  23. position = pj
  24. ) +
  25. geom_path(aes(x = name_num, group = ID),
  26. alpha = 0.5,
  27. position = pj
  28. )

分开盒图和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:

确定