如何标注出界点?

问题描述 投票:0回答:1

对于x或y有

Inf
值的情况,如何标记该点?

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

ggplot2 ggrepel
1个回答
0
投票

用一个大的有限数替换值,然后手动调整可能就是您想要的

library(ggplot2)
library(ggrepel)


df <- data.frame(x = 0, y = 1e6, row = "oob")


ggplot(df, aes(x, y)) +
    geom_point() +
    scale_y_continuous(limits = c(-1, 1), oob = scales::squish) +
    ggrepel::geom_label_repel(aes(label = ifelse(y == 1e6, "Inf", row)),
        nudge_y = -0.2, nudge_x = 0.2
    ) +
    coord_cartesian(clip = "off")
© www.soinside.com 2019 - 2024. All rights reserved.