对于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")
用一个大的有限数替换值,然后手动调整可能就是您想要的
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")