如何添加一个在绘图组成部分计算数值的注释?

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

How to add an annotation that calculates a value as part of plot composition?

问题

使用ggplot,我想要添加一个_注释_,描述轴的平均值,并且希望进行即时计算,而不是将值分配给专用对象的预先计算。

示例

考虑以下按cut划分的carat直方图:

  1. library(ggplot2)
  2. library(dplyr)
  3. diamonds |>
  4. filter(depth > 60) |>
  5. ggplot(aes(carat)) +
  6. geom_histogram() +
  7. stat_summary(aes(xintercept = after_stat(x), y = 0),
  8. fun = mean,
  9. geom = "vline",
  10. orientation = "y",
  11. color = "red") +
  12. facet_wrap(~cut)

如何添加一个在绘图组成部分计算数值的注释?

创建于2023-07-13,使用reprex v2.0.2

期望的输出

我是否可以在ggplot组合中添加另一行代码,以添加一个注释,显示"平均克拉 = mean(carat)",并且它将按facet进行平均计算?

伪代码

如何添加一个在绘图组成部分计算数值的注释?

英文:

Using ggplot, I want to add an annotation that describes the mean value of an axis, and I wish to make this calculation on the fly, rather than a preemptive calculation that assigns a value to a dedicated object.

Example

Consider the following carat histogram by cut:

  1. library(ggplot2)
  2. library(dplyr)
  3. #>
  4. #> Attaching package: 'dplyr'
  5. #> The following objects are masked from 'package:stats':
  6. #>
  7. #> filter, lag
  8. #> The following objects are masked from 'package:base':
  9. #>
  10. #> intersect, setdiff, setequal, union
  11. diamonds |>
  12. filter(depth > 60) |>
  13. ggplot(aes(carat)) +
  14. geom_histogram() +
  15. stat_summary(aes(xintercept = after_stat(x), y = 0),
  16. fun = mean,
  17. geom = "vline",
  18. orientation = "y",
  19. color = "red") +
  20. facet_wrap(~cut)
  21. #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

如何添加一个在绘图组成部分计算数值的注释?<!-- -->

<sup>Created on 2023-07-13 with reprex v2.0.2</sup>

Desired Output

Can I tuck another line of code to the ggplot composition that will add an annotation for &quot;Mean Carat = mean(carat)&quot; and it will do the mean calculation per facet?

Pseudo Plot

如何添加一个在绘图组成部分计算数值的注释?

答案1

得分: 3

基本上与vline相同,但我们必须使用geom="text"并在label aes上映射:

  1. library(ggplot2)
  2. library(dplyr, warn = FALSE)
  3. diamonds |>
  4. filter(depth > 60) |>
  5. ggplot(aes(carat)) +
  6. geom_histogram() +
  7. stat_summary(aes(xintercept = after_stat(x), y = 0),
  8. fun = mean,
  9. geom = "vline",
  10. orientation = "y",
  11. color = "red"
  12. ) +
  13. stat_summary(
  14. aes(
  15. label = after_stat(paste0("均值克拉 = ", round(x, 3))),
  16. y = 6000
  17. ),
  18. fun = mean,
  19. geom = "text",
  20. orientation = "y",
  21. color = "red",
  22. hjust = 0,
  23. position = position_nudge(x = .1)
  24. ) +
  25. facet_wrap(~cut)

如何添加一个在绘图组成部分计算数值的注释?

英文:

It's basically the same as for the vline but instead we have to use geom=&quot;text&quot; and map on the label aes:

  1. library(ggplot2)
  2. library(dplyr, warn = FALSE)
  3. diamonds |&gt;
  4. filter(depth &gt; 60) |&gt;
  5. ggplot(aes(carat)) +
  6. geom_histogram() +
  7. stat_summary(aes(xintercept = after_stat(x), y = 0),
  8. fun = mean,
  9. geom = &quot;vline&quot;,
  10. orientation = &quot;y&quot;,
  11. color = &quot;red&quot;
  12. ) +
  13. stat_summary(
  14. aes(
  15. label = after_stat(paste0(&quot;Mean Carat = &quot;, round(x, 3))),
  16. y = 6000
  17. ),
  18. fun = mean,
  19. geom = &quot;text&quot;,
  20. orientation = &quot;y&quot;,
  21. color = &quot;red&quot;,
  22. hjust = 0,
  23. position = position_nudge(x = .1)
  24. ) +
  25. facet_wrap(~cut)

如何添加一个在绘图组成部分计算数值的注释?

huangapple
  • 本文由 发表于 2023年7月13日 18:06:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678198.html
匿名

发表评论

匿名网友

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

确定