我有一个 6 面 ggplot2 条形图。我希望每个方面都用其各自的平均值进行注释。像这样:
x̄ = 0.25
所以我一直在努力:
geom_text (data = averages, inherit.aes = FALSE,
aes (label = expression (bar (x) == round (Mean, 2)), x = 30, y = 61.5)) +
但我总是得到这样的回报:
Don't know how to automatically pick scale for object of type <expression>. Defaulting to continuous.
Error in `geom_text()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 2nd layer.
Caused by error in `compute_aesthetics()`:
! Aesthetics are not valid data columns.
✖ The following aesthetics are invalid:
✖ `label = expression(bar(x) == round(Mean, 2))`
ℹ Did you mistype the name of a data column or forget to add `after_stat()`?
Run `rlang::last_trace()` to see where the error occurred.
我还尝试了这两篇文章中的大多数建议:在绘图标签中组合粘贴()和表达式()函数和将数学符号和下标与常规字母混合
有什么办法可以做到这一点吗?
一种选择是将
paste(0)
与 parse=TRUE
一起使用,如下所示:
averages <- data.frame(
facet = letters[1:6],
Mean = 1:6
)
library(ggplot2)
ggplot() +
geom_text(
data = averages, inherit.aes = FALSE,
aes(label = paste0("bar(x) == ", round(Mean, 2)), x = 30, y = 61.5),
parse = TRUE
) +
facet_wrap(~facet)