英文:
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` 选择更好的值。
<!-- -->
ggplotly()
有一个很好的技巧,允许我使用虚拟美学特征在工具提示中显示。 下面,我使用 text
美学特征来实现这一目的。 但是,glue()
与 after_stat()
不兼容。 我该如何修复这个问题?
library(ggplot2)
#> 警告: package 'ggplot2' was built under R version 4.2.2
p <-
mtcars |>
ggplot() +
geom_histogram(
aes(
x = disp,
text = glue::glue('{after_stat(count)}')
)
)
#> 警告 in geom_histogram(aes(x = disp, text =
#> glue::glue("{after_stat(count)}"))): 忽略未知的美学特征: text
plotly::ggplotly(p)
#> 错误 in after_stat(count): 找不到对象 'count'
英文:
I would like to create a plot using plotly::ggplotly()
. This works just fine.
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.2.2
p <-
mtcars |>
ggplot() +
geom_histogram(
aes(
x = disp,
text = after_stat(count)
)
)
#> Warning in geom_histogram(aes(x = disp, text = after_stat(count))): Ignoring
#> unknown aesthetics: text
plotly::ggplotly(p)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
<!-- -->
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)
#> Warning: package 'ggplot2' was built under R version 4.2.2
p <-
mtcars |>
ggplot() +
geom_histogram(
aes(
x = disp,
text = glue::glue('{after_stat(count)}')
)
)
#> Warning in geom_histogram(aes(x = disp, text =
#> glue::glue("{after_stat(count)}"))): Ignoring unknown aesthetics: text
plotly::ggplotly(p)
#> Error in after_stat(count): object 'count' not found
答案1
得分: 1
这段代码在 aes
之外使用了 text
(这让我感到惊讶):
p <-
ggplot(data = mtcars, aes(x = disp), text = glue::glue('{after_stat({count})}')) +
geom_histogram()
plotly::ggplotly(p)
英文:
It work with text
outside aes
(that surprises me):
p <-
ggplot(data = mtcars, aes(x = disp), text = glue::glue('{after_stat({count})}')) +
geom_histogram()
plotly::ggplotly(p)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论