代码之家  ›  专栏  ›  技术社区  ›  Indrajeet Patil

ggplot2中基于“…count…”变量的“geom_label”的y值

  •  2
  • Indrajeet Patil  · 技术社区  · 6 年前

    我想创建一个柱状图,其中有一条表示平均值的垂直线和一个附在这条线上的标签,它给出了平均值的确切值。

    我可以很容易地创建一个垂直线的基本直方图。

    # 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轴的中间?

    1 回复  |  直到 6 年前
        1
  •  5
  •   Z.Lin    6 年前

    您可以从 plot ,替换硬编码 y = 5 在你的代码中 y = mean(layer_scales(plot)$y$range$range) .

    这样,如果参数发生变化,就会考虑到比例的变化。

    # 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