R: 将 geom_errorbarh 重新排序放入 ggplot 中

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

R: Reordering geom_errorbarh put into gglot

问题

我有以下的CSV文件:

  1. group;response
  2. stepOne;107
  3. stepOne;946
  4. stepTwo;184
  5. stepTwo;456
  6. ...

我正在将它读入数据框中,计算统计数据,并将该统计数据反映在误差条形图上。以下是代码:

  1. # 加载所需的库
  2. library(ggplot2)
  3. library(dplyr)
  4. library(viridis)
  5. # 读取输入的CSV文件
  6. data <- read.csv("~/Documents/r/input_file.csv", sep=";", header=TRUE)
  7. x_axis_name <- names(data)[2]
  8. y_axis_name <- names(data)[1]
  9. title <- sprintf("置信区间:%s", names(data)[1])
  10. # 为每个组计算均值和置信区间
  11. group_stats <- data %>%
  12. group_by(group) %>%
  13. summarise(
  14. mean = mean(response),
  15. lower_ci_90 = mean - qt(0.90, df = n() - 1) * (sd(response) / sqrt(n()-1)),
  16. upper_ci_90 = mean + qt(0.90, df = n() - 1) * (sd(response) / sqrt(n()-1)),
  17. lower_ci_95 = mean - qt(0.95, df = n() - 1) * (sd(response) / sqrt(n()-1)),
  18. upper_ci_95 = mean + qt(0.95, df = n() - 1) * (sd(response) / sqrt(n()-1)),
  19. lower_ci_99 = mean - qt(0.99, df = n() - 1) * (sd(response) / sqrt(n()-1)),
  20. upper_ci_99 = mean + qt(0.99, df = n() - 1) * (sd(response) / sqrt(n()-1)),
  21. lower_ci_999 = mean - qt(0.999, df = n() - 1) * (sd(response) / sqrt(n()-1)),
  22. upper_ci_999 = mean + qt(0.999, df = n() - 1) * (sd(response) / sqrt(n()-1))
  23. ) %>%
  24. arrange(desc(group))
  25. # 从group_stats中找到绝对最大值
  26. limit_x_left = 0
  27. limit_x_right <- 1.15*max(unlist(group_stats[, sapply(group_stats, is.numeric)]))
  28. # 生成图形
  29. graph <- ggplot(group_stats, aes(y = group[order(group, decreasing=TRUE)], x = mean, color = group)) +
  30. geom_errorbarh(aes(xmin = lower_ci_90, xmax = upper_ci_90), height = 0.0, linewidth = 2.5, alpha = 0.25) +
  31. geom_errorbarh(aes(xmin = lower_ci_95, xmax = upper_ci_95), height = 0.0, linewidth = 2.5, alpha = 0.25) +
  32. geom_errorbarh(aes(xmin = lower_ci_99, xmax = upper_ci_99), height = 0.0, linewidth = 2.5, alpha = 0.25) +
  33. geom_point(size = 3) +
  34. labs(x = x_axis_name, y = y_axis_name, title = title, color = "图例") +
  35. xlim(limit_x_left, limit_x_right) +
  36. scale_color_manual(values = group_colors)
  37. # 显示图形
  38. print(graph)

无论我如何初始化group_stats数据框,ggplot都会按字母顺序降序排列条形。试图在ggplot内部重新排序不起作用:

  1. y = group[order(group, decreasing=FALSE)]
  2. y = group[order(group, decreasing=TRUE)]
  3. 被忽略。
  4. 但是,如果我使用
  5. y = group[order(mean, decreasing=TRUE)]
  6. 值将按均值完美排序。
  7. 所以,有没有办法改变误差条的排序方式?我想按字母升序排列它们。
  8. <details>
  9. <summary>英文:</summary>
  10. I have the following CSV file:
  11. ```
  12. group;response
  13. stepOne;107
  14. stepOne;946
  15. stepTwo;184
  16. stepTwo;456
  17. ...
  18. ```
  19. I am reading it into dataframe, calculate the stats and reflect that stats on graph as errorbarsh. The code is the following
  20. ```
  21. # Load required libraries
  22. library(ggplot2)
  23. library(dplyr)
  24. library(viridis)
  25. # Read the input CSV file
  26. data &lt;- read.csv(&quot;~/Documents/r/input_file.csv&quot;, sep=&quot;;&quot;, header=TRUE)
  27. x_axis_name &lt;- names(data)[2]
  28. y_axis_name &lt;- names(data)[1]
  29. title &lt;- sprintf(&quot;Confidence interval for %s&quot;, names(data)[1])
  30. # Calculate mean and confidence intervals for each group
  31. group_stats &lt;- data %&gt;%
  32. group_by(group) %&gt;%
  33. summarise(
  34. mean = mean(response),
  35. lower_ci_90 = mean - qt(0.90, df = n() - 1) * (sd(response) / sqrt(n()-1)),
  36. upper_ci_90 = mean + qt(0.90, df = n() - 1) * (sd(response) / sqrt(n()-1)),
  37. lower_ci_95 = mean - qt(0.95, df = n() - 1) * (sd(response) / sqrt(n()-1)),
  38. upper_ci_95 = mean + qt(0.95, df = n() - 1) * (sd(response) / sqrt(n()-1)),
  39. lower_ci_99 = mean - qt(0.99, df = n() - 1) * (sd(response) / sqrt(n()-1)),
  40. upper_ci_99 = mean + qt(0.99, df = n() - 1) * (sd(response) / sqrt(n()-1)),
  41. lower_ci_999 = mean - qt(0.999, df = n() - 1) * (sd(response) / sqrt(n()-1)),
  42. upper_ci_999 = mean + qt(0.999, df = n() - 1) * (sd(response) / sqrt(n()-1))
  43. ) %&gt;%
  44. arrange(desc(group))
  45. # Find absolute maximum from group_stats
  46. limit_x_left = 0
  47. limit_x_right &lt;- 1.15*max(unlist(group_stats[, sapply(group_stats, is.numeric)]))
  48. # Generate the graph
  49. graph &lt;- ggplot(group_stats, aes(y = group[order(group, decreasing=TRUE)], x = mean, color = group)) +
  50. geom_errorbarh(aes(xmin = lower_ci_90, xmax = upper_ci_90), height = 0.0, linewidth = 2.5, alpha = 0.25) +
  51. geom_errorbarh(aes(xmin = lower_ci_95, xmax = upper_ci_95), height = 0.0, linewidth = 2.5, alpha = 0.25) +
  52. geom_errorbarh(aes(xmin = lower_ci_99, xmax = upper_ci_99), height = 0.0, linewidth = 2.5, alpha = 0.25) +
  53. geom_point(size = 3) +
  54. labs(x = x_axis_name, y = y_axis_name, title = title, color = &quot;Legend&quot;) +
  55. xlim(limit_x_left, limit_x_right) +
  56. scale_color_manual(values = group_colors)
  57. # Display the graph
  58. print(graph)
  59. ```
  60. No matter how I initially arrange group_stats dataframe, ggplot will put bars ordering them alphabetically DESC.
  61. Trying to reorder it inside ggplot leads to nothing:
  62. y = group[order(group, decreasing=FALSE)]
  63. or
  64. y = group[order(group, decreasing=TRUE)]
  65. is being ignored.
  66. [![enter image description here][1]][1]
  67. However if I put
  68. y = group[order(mean, decreasing=TRUE)] the values are being sorted by mean perfectly.
  69. [![enter image description here][2]][2]
  70. So, is there any way to change order of errorbars are being put onto plot? I want to order them alphabetically ASC.
  71. [1]: https://i.stack.imgur.com/nc69Q.png
  72. [2]: https://i.stack.imgur.com/0fM9B.png
  73. </details>
  74. # 答案1
  75. **得分**: 1
  76. 根据 @jared_mamrot,使用 y = fct_rev(group) 而不是 y = group[order(group, decreasing=TRUE)] 可以解决这个问题。需要安装 forcats 包:
  77. ```install.packages("forcats")```
  78. 或者安装 tidyverse 包:
  79. ```install.packages("tidyverse")```
  80. 然后在代码中添加:
  81. ```library(forcats)```
  82. 感谢 @jared_mamrot
  83. <details>
  84. <summary>英文:</summary>
  85. Per @jared_mamrot, y = fct_rev(group) instead of y = group[order(group, decreasing=TRUE)] solves the issue. Need to install forcats:
  86. ```install.packages(&quot;forcats&quot;)```
  87. (or ```install.packages(&quot;tidyverse&quot;)```)
  88. and add
  89. ```library(forcats)```
  90. to the code.
  91. Thanks @jared_mamrot
  92. </details>

huangapple
  • 本文由 发表于 2023年6月22日 07:22:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527720.html
匿名

发表评论

匿名网友

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

确定