我想创建一个直方图,其中有一条垂直线表示平均值,并在该线上附加了一个给出平均值的精确值的标签。

我可以轻松创建一条带有垂直线的基本直方图。



# 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`.




现在的问题是,我正在为ygeom_label)的y = 5值进行硬编码。这不是理想的,因为如果我更改数据或变量或二进制宽度,y = 5将不再是y轴的(大约)中间。我尝试设置y = max(..count..)/2,但这会导致以下错误:


FUN(X [[i]],...)中的错误:找不到对象“ count”


把它们加起来:
在这种情况下,如何自动选择ygeom_label值,以使无论计数范围如何,标签始终位于Y轴的中心?

最佳答案

通过用plot替换代码中的硬编码y = 5,可以从y = mean(layer_scales(plot)$y$range$range)获取当前的y轴范围。

这样,如果参数发生变化,则说明比例发生了变化。

# layer_scales(plot) gives the scale information for plot
> layer_scales(plot)$y
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
<ScaleContinuousPosition>
 Range:     0 --   12
 Limits:    0 --   12

# this is the actual vector for y-axis scale range
> layer_scales(plot)$y$range$range
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
[1]  0 12

# this is the y-axis midpoint value
> mean(layer_scales(plot)$y$range$range)
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
[1] 6

关于r - ggplot2中基于`.count..`变量的`geom_label`的y值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52022833/

10-10 05:50