After_stat() 和 glue() 不能一起使用。

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

After_stat() and glue() do not work together

问题

我想使用 plotly::ggplotly() 创建一个图表。 这个方法运行正常。

  1. library(ggplot2)
  2. #> 警告: package 'ggplot2' was built under R version 4.2.2
  3. p <-
  4. mtcars |>
  5. ggplot() +
  6. geom_histogram(
  7. aes(
  8. x = disp,
  9. text = after_stat(count)
  10. )
  11. )
  12. #> 警告 in geom_histogram(aes(x = disp, text = after_stat(count))): 忽略未知的美学特征: text
  13. plotly::ggplotly(p)
  14. #> `stat_bin()` 使用 `bins = 30`。 使用 `binwidth` 选择更好的值。

After_stat() 和 glue() 不能一起使用。<!-- -->

ggplotly() 有一个很好的技巧,允许我使用虚拟美学特征在工具提示中显示。 下面,我使用 text 美学特征来实现这一目的。 但是,glue()after_stat() 不兼容。 我该如何修复这个问题?

  1. library(ggplot2)
  2. #&gt; 警告: package &#39;ggplot2&#39; was built under R version 4.2.2
  3. p &lt;-
  4. mtcars |&gt;
  5. ggplot() +
  6. geom_histogram(
  7. aes(
  8. x = disp,
  9. text = glue::glue(&#39;{after_stat(count)}&#39;)
  10. )
  11. )
  12. #&gt; 警告 in geom_histogram(aes(x = disp, text =
  13. #&gt; glue::glue(&quot;{after_stat(count)}&quot;))): 忽略未知的美学特征: text
  14. plotly::ggplotly(p)
  15. #&gt; 错误 in after_stat(count): 找不到对象 &#39;count&#39;
英文:

I would like to create a plot using plotly::ggplotly(). This works just fine.

  1. library(ggplot2)
  2. #&gt; Warning: package &#39;ggplot2&#39; was built under R version 4.2.2
  3. p &lt;-
  4. mtcars |&gt;
  5. ggplot() +
  6. geom_histogram(
  7. aes(
  8. x = disp,
  9. text = after_stat(count)
  10. )
  11. )
  12. #&gt; Warning in geom_histogram(aes(x = disp, text = after_stat(count))): Ignoring
  13. #&gt; unknown aesthetics: text
  14. plotly::ggplotly(p)
  15. #&gt; `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

After_stat() 和 glue() 不能一起使用。<!-- -->

There is a nice trick with ggplotly() that allows me to use dummy aesthetics to display in the tooltip. Below, I use the text aesthetic for this purpose. However, glue() doesn't work with after_stat(). How can I fix this?

  1. library(ggplot2)
  2. #&gt; Warning: package &#39;ggplot2&#39; was built under R version 4.2.2
  3. p &lt;-
  4. mtcars |&gt;
  5. ggplot() +
  6. geom_histogram(
  7. aes(
  8. x = disp,
  9. text = glue::glue(&#39;{after_stat(count)}&#39;)
  10. )
  11. )
  12. #&gt; Warning in geom_histogram(aes(x = disp, text =
  13. #&gt; glue::glue(&quot;{after_stat(count)}&quot;))): Ignoring unknown aesthetics: text
  14. plotly::ggplotly(p)
  15. #&gt; Error in after_stat(count): object &#39;count&#39; not found

答案1

得分: 1

这段代码在 aes 之外使用了 text(这让我感到惊讶):

  1. p &lt;-
  2. ggplot(data = mtcars, aes(x = disp), text = glue::glue(&#39;{after_stat({count})}&#39;)) +
  3. geom_histogram()
  4. plotly::ggplotly(p)
英文:

It work with text outside aes (that surprises me):

  1. p &lt;-
  2. ggplot(data = mtcars, aes(x = disp), text = glue::glue(&#39;{after_stat({count})}&#39;)) +
  3. geom_histogram()
  4. plotly::ggplotly(p)

huangapple
  • 本文由 发表于 2023年6月2日 10:27:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386786.html
匿名

发表评论

匿名网友

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

确定