Search code examples
rggplot2ggrepel

How to label out of bound point?


For x or y have Inf value, how to label the point?

ggplot(data.frame(x = 0, y = Inf, row = "oob"), aes(x, y)) +
    geom_point() +
    scale_y_continuous(limits = c(-1, 1), oob = scales::squish) +
    ggrepel::geom_label_repel(aes(label = row),
        nudge_y = -0.2, nudge_x = 0.2
    ) +
    coord_cartesian(clip = "off")

enter image description here


Solution

  • The issue is due to a change in ggrepel 0.9.5 (see here) but has been fixed in the dev version. If you need to use ggrepel then use the dev version or switch to an older version (e.g. 0.9.4).

    library(ggplot2)
    library(ggrepel)
    
    packageVersion("ggrepel")
    #> [1] '0.9.5.9999'
    
    ggplot(data.frame(x = 0, y = Inf, row = "oob"), aes(x, y)) +
      geom_point() +
      scale_y_continuous(
        limits = c(-1, 1),
        oob = scales::squish
      ) +
      geom_label_repel(
        aes(label = row),
        nudge_y = -0.2, nudge_x = 0.2
      ) +
      coord_cartesian(clip = "off")