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

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

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

问题

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

示例

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

library(ggplot2)
library(dplyr)

diamonds |> 
  filter(depth > 60) |> 
  ggplot(aes(carat)) +
  geom_histogram() +
  stat_summary(aes(xintercept = after_stat(x), y = 0), 
               fun = mean, 
               geom = "vline", 
               orientation = "y", 
               color = "red") +
  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:

library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

diamonds |> 
  filter(depth > 60) |> 
  ggplot(aes(carat)) +
  geom_histogram() +
  stat_summary(aes(xintercept = after_stat(x), y = 0), 
               fun = mean, 
               geom = "vline", 
               orientation = "y", 
               color = "red") +
  facet_wrap(~cut)
#> `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上映射:

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

diamonds |>
  filter(depth > 60) |>
  ggplot(aes(carat)) +
  geom_histogram() +
  stat_summary(aes(xintercept = after_stat(x), y = 0),
    fun = mean,
    geom = "vline",
    orientation = "y",
    color = "red"
  ) +
  stat_summary(
    aes(
      label = after_stat(paste0("均值克拉 = ", round(x, 3))),
      y = 6000
    ),
    fun = mean,
    geom = "text",
    orientation = "y",
    color = "red",
    hjust = 0,
    position = position_nudge(x = .1)
  ) +
  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:

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

diamonds |&gt;
  filter(depth &gt; 60) |&gt;
  ggplot(aes(carat)) +
  geom_histogram() +
  stat_summary(aes(xintercept = after_stat(x), y = 0),
    fun = mean,
    geom = &quot;vline&quot;,
    orientation = &quot;y&quot;,
    color = &quot;red&quot;
  ) +
  stat_summary(
    aes(
      label = after_stat(paste0(&quot;Mean Carat = &quot;, round(x, 3))),
      y = 6000
    ),
    fun = mean,
    geom = &quot;text&quot;,
    orientation = &quot;y&quot;,
    color = &quot;red&quot;,
    hjust = 0,
    position = position_nudge(x = .1)
  ) +
  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:

确定