R 将标签放置在散布的上方和下方
我正在使用R 将标签放置在散布的上方和下方,r,ggplot2,ggrepel,R,Ggplot2,Ggrepel,我正在使用ggrepel向我的ggplot图形添加标签。为了更好地利用绘图中的空间,我想在“上_值”上方添加一个标签,并在“下_值”下方沿x轴穿插添加以下标签 问题是,即使我相应地设置了nudge_y,底部标签仍会显示在上部标签的旁边 以下是我尝试过的: set.seed(5) library(ggplot2) library(ggrepel) library(dplyr) d <- data.frame( x = 1:60, y = c( sample(x = seq
ggrepel
向我的ggplot
图形添加标签。为了更好地利用绘图中的空间,我想在“上_值”上方添加一个标签,并在“下_值”下方沿x轴穿插添加以下标签
问题是,即使我相应地设置了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))
set.seed(5)
图书馆(GG2)
图书馆(ggrepel)
图书馆(dplyr)
d=0.8]%
变异(lab_set=rep(c(1,2),nrow(.)/2))
ggplot(数据=d,aes(x=x,y=y),show.legend=TRUE)+
几何线()
几何点(数据=过滤器(d,!is.na(labs)),color=“红色”)+
#将标签放置在1.2上方
geom_标签_排斥(
数据=过滤器(d_lab,lab_set==“1”)、aes(标签=实验室),
segment.color=“黑色”,
箭头=箭头(长度=单位(0.01,“npc”),type=“closed”,ends=“first”),
填充=α(c(“白色”),0.5),
#show.legend=FALSE,
color=“黑色”,
direction=“x”,
微移_y=1.2
) +
#将标签放置在0.8-->以下不会对微移作出响应!!!
geom_标签_排斥(
数据=过滤器(d_lab,lab_set==“2”),aes(标签=实验室),
segment.color=“黑色”,
箭头=箭头(长度=单位(0.01,“npc”),type=“closed”,ends=“first”),
填充=α(c(“白色”),0.5),
#show.legend=FALSE,
color=“黑色”,
direction=“x”,
微移_y=0.8
) +
连续刻度(限值=c(NA,1.2))
尝试在第二个geom_label_repel
调用中使用负值来向下推:
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))