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

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

After_stat() and glue() do not work together

问题

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

library(ggplot2)
#> 警告: package 'ggplot2' was built under R version 4.2.2

p <-
  mtcars |>
  ggplot() +
  geom_histogram(
    aes(
      x = disp,
      text = after_stat(count)
    )
  )
#> 警告 in geom_histogram(aes(x = disp, text = after_stat(count))): 忽略未知的美学特征: text

plotly::ggplotly(p)
#> `stat_bin()` 使用 `bins = 30`。 使用 `binwidth` 选择更好的值。

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

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

library(ggplot2)
#&gt; 警告: package &#39;ggplot2&#39; was built under R version 4.2.2

p &lt;-
  mtcars |&gt;
  ggplot() +
  geom_histogram(
    aes(
      x = disp,
      text = glue::glue(&#39;{after_stat(count)}&#39;)
    )
  )
#&gt; 警告 in geom_histogram(aes(x = disp, text =
#&gt; glue::glue(&quot;{after_stat(count)}&quot;))): 忽略未知的美学特征: text

plotly::ggplotly(p)
#&gt; 错误 in after_stat(count): 找不到对象 &#39;count&#39;
英文:

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

library(ggplot2)
#&gt; Warning: package &#39;ggplot2&#39; was built under R version 4.2.2

p &lt;-
  mtcars |&gt;
  ggplot() +
  geom_histogram(
    aes(
      x = disp,
      text = after_stat(count)
    )
  )
#&gt; Warning in geom_histogram(aes(x = disp, text = after_stat(count))): Ignoring
#&gt; unknown aesthetics: text

plotly::ggplotly(p)
#&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?

library(ggplot2)
#&gt; Warning: package &#39;ggplot2&#39; was built under R version 4.2.2

p &lt;-
  mtcars |&gt;
  ggplot() +
  geom_histogram(
    aes(
      x = disp,
      text = glue::glue(&#39;{after_stat(count)}&#39;)
    )
  )
#&gt; Warning in geom_histogram(aes(x = disp, text =
#&gt; glue::glue(&quot;{after_stat(count)}&quot;))): Ignoring unknown aesthetics: text

plotly::ggplotly(p)
#&gt; Error in after_stat(count): object &#39;count&#39; not found

答案1

得分: 1

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

p &lt;-
   ggplot(data = mtcars, aes(x = disp), text = glue::glue(&#39;{after_stat({count})}&#39;)) +
   geom_histogram()

plotly::ggplotly(p)
英文:

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

p &lt;-
   ggplot(data = mtcars, aes(x = disp), text = glue::glue(&#39;{after_stat({count})}&#39;)) +
   geom_histogram()
 
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:

确定