上下穿插放置标签
Place labels above and below interspersed
我正在使用 ggrepel
向我的 ggplot
图表添加标签。为了更好地使用图中的 space,我想在“upper_value”上方添加一个标签,并在 x 轴的“bottom_value”下方添加一个标签,散布。
问题是底部标签一直显示在顶部标签旁边,即使我相应地设置了 nudge_y
也是如此。
这是我试过的方法:
set.seed(5)
library(ggplot2)
library(ggrepel)
library(dplyr)
d <- data.frame(
x = 1:60,
y = c(
sample(x = seq(0,0.2,0.01), size = 20, replace = TRUE),
sample(x = seq(0.8,1,0.01), size = 20, replace = TRUE),
sample(x = seq(0,0.2,0.01), size = 20, replace = TRUE)
)
)
# add labels for high scores
d$labs[d$y >= 0.8] <- paste0("label_",letters[1:20])
# add a column to intersperse labels (above=1 and below=2)
d_lab <- d %>%
filter(!is.na(labs) ) %>%
mutate(lab_set = rep(c(1,2), nrow(.)/2) )
ggplot(data = d, aes(x = x, y = y), show.legend = TRUE) +
geom_line() +
geom_point(data = filter(d, !is.na(labs)), color = "red") +
# place labels above 1.2
geom_label_repel(
data = filter(d_lab, lab_set == "1"),aes(label = labs),
segment.color = "black",
arrow = arrow(length = unit(0.01, "npc"), type = "closed", ends = "first"),
fill = alpha(c("white"),0.5),
#show.legend = FALSE,
color = "black",
direction = "x",
nudge_y = 1.2
) +
# place labels below 0.8 --> does not respond to nudge_y!!!
geom_label_repel(
data = filter(d_lab, lab_set == "2"),aes(label = labs),
segment.color = "black",
arrow = arrow(length = unit(0.01, "npc"), type = "closed", ends = "first"),
fill = alpha(c("white"),0.5),
#show.legend = FALSE,
color = "black",
direction = "x",
nudge_y = 0.8
) +
scale_y_continuous(limits = c(NA, 1.2))
尝试在第二个 geom_label_repel
调用中为 nudge_y
使用负值以向下微调:
ggplot(data = d, aes(x = x, y = y), show.legend = TRUE) +
geom_line() +
geom_point(data = filter(d, !is.na(labs)), color = "red") +
# place labels above 1.2
geom_label_repel(
data = filter(d_lab, lab_set == "1"),aes(label = labs),
segment.color = "black",
arrow = arrow(length = unit(0.01, "npc"), type = "closed", ends = "first"),
fill = alpha(c("white"),0.5),
#show.legend = FALSE,
color = "black",
direction = "x",
nudge_y = 1.2
) +
# place labels below 0.8 --> does not respond to nudge_y!!!
geom_label_repel(
data = filter(d_lab, lab_set == "2"),aes(label = labs),
segment.color = "black",
arrow = arrow(length = unit(0.01, "npc"), type = "closed", ends = "first"),
fill = alpha(c("white"),0.5),
#show.legend = FALSE,
color = "black",
direction = "x",
nudge_y = -0.25 # USE NEGATIVE VALUE HERE
) +
scale_y_continuous(limits = c(NA, 1.2))
我正在使用 ggrepel
向我的 ggplot
图表添加标签。为了更好地使用图中的 space,我想在“upper_value”上方添加一个标签,并在 x 轴的“bottom_value”下方添加一个标签,散布。
问题是底部标签一直显示在顶部标签旁边,即使我相应地设置了 nudge_y
也是如此。
这是我试过的方法:
set.seed(5)
library(ggplot2)
library(ggrepel)
library(dplyr)
d <- data.frame(
x = 1:60,
y = c(
sample(x = seq(0,0.2,0.01), size = 20, replace = TRUE),
sample(x = seq(0.8,1,0.01), size = 20, replace = TRUE),
sample(x = seq(0,0.2,0.01), size = 20, replace = TRUE)
)
)
# add labels for high scores
d$labs[d$y >= 0.8] <- paste0("label_",letters[1:20])
# add a column to intersperse labels (above=1 and below=2)
d_lab <- d %>%
filter(!is.na(labs) ) %>%
mutate(lab_set = rep(c(1,2), nrow(.)/2) )
ggplot(data = d, aes(x = x, y = y), show.legend = TRUE) +
geom_line() +
geom_point(data = filter(d, !is.na(labs)), color = "red") +
# place labels above 1.2
geom_label_repel(
data = filter(d_lab, lab_set == "1"),aes(label = labs),
segment.color = "black",
arrow = arrow(length = unit(0.01, "npc"), type = "closed", ends = "first"),
fill = alpha(c("white"),0.5),
#show.legend = FALSE,
color = "black",
direction = "x",
nudge_y = 1.2
) +
# place labels below 0.8 --> does not respond to nudge_y!!!
geom_label_repel(
data = filter(d_lab, lab_set == "2"),aes(label = labs),
segment.color = "black",
arrow = arrow(length = unit(0.01, "npc"), type = "closed", ends = "first"),
fill = alpha(c("white"),0.5),
#show.legend = FALSE,
color = "black",
direction = "x",
nudge_y = 0.8
) +
scale_y_continuous(limits = c(NA, 1.2))
尝试在第二个 geom_label_repel
调用中为 nudge_y
使用负值以向下微调:
ggplot(data = d, aes(x = x, y = y), show.legend = TRUE) +
geom_line() +
geom_point(data = filter(d, !is.na(labs)), color = "red") +
# place labels above 1.2
geom_label_repel(
data = filter(d_lab, lab_set == "1"),aes(label = labs),
segment.color = "black",
arrow = arrow(length = unit(0.01, "npc"), type = "closed", ends = "first"),
fill = alpha(c("white"),0.5),
#show.legend = FALSE,
color = "black",
direction = "x",
nudge_y = 1.2
) +
# place labels below 0.8 --> does not respond to nudge_y!!!
geom_label_repel(
data = filter(d_lab, lab_set == "2"),aes(label = labs),
segment.color = "black",
arrow = arrow(length = unit(0.01, "npc"), type = "closed", ends = "first"),
fill = alpha(c("white"),0.5),
#show.legend = FALSE,
color = "black",
direction = "x",
nudge_y = -0.25 # USE NEGATIVE VALUE HERE
) +
scale_y_continuous(limits = c(NA, 1.2))