我想创建一个柱状图,其中有一条表示平均值的垂直线和一个附在这条线上的标签,它给出了平均值的确切值。
我可以很容易地创建一个垂直线的基本直方图。
# needed library
library(ggplot2)
# mean to be used later
x_mean <- mean(x = iris$Sepal.Length, na.rm = TRUE)
# creating basic plot with line for mean
(
plot <- ggplot(data = iris,
mapping = aes(x = Sepal.Length)) +
stat_bin(
col = "black",
alpha = 0.7,
na.rm = TRUE,
mapping = aes(y = ..count..,
fill = ..count..)
) +
geom_vline(
xintercept = x_mean,
linetype = "dashed",
color = "red",
na.rm = TRUE
) +
scale_fill_gradient(name = "count",
low = "white",
high = "white") +
guides(fill = FALSE)
)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
现在,我可以使用以下代码向此行添加标签:
# adding label to the line
plot +
geom_label(mapping = aes(
label = list(bquote("mean" == ~ .(
format(round(x_mean, 2), nsmall = 2)
))),
x = x_mean,
y = 5 # how to automate this value choice?
),
parse = TRUE)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
现在的问题是我正在硬编码
y
-价值
geom_label
(
y = 5
). 这并不理想,因为如果我更改数据、变量或binwidth,
y=5
将不再是y轴的(近似)中间。我试着设置
y = max(..count..)/2
,但这会导致以下错误:
趣味错误(X[[i]],…):未找到对象“count”
总而言之:
如何选择
是的
价值
几何标签
在这种情况下是自动的,所以不管计数范围如何,标签总是在Y轴的中间?