我使用grid::segmentsGrob(arrow)向ggplot添加箭头。我用x轴坐标定义箭头的位置。出于某种原因,箭头的高度根据箭头的长度进行缩放。我希望箭头占据相同的高度,只缩短箭头的长度。我是否缺少了一个高度参数?
library(ggplot2)
library(grid)
df <- data.frame(
Position=(c(1:5000)),
Signal=(c((rep(c(5),times=2000)), (rep(c(100),times=1000)), (rep(c(5),times=2000))))
)
Plotv1 <- ggplot()+
geom_line(data = df, aes(x=Position, y=Signal, col = "#000000"))+
coord_cartesian(clip="off") +
theme(axis.text.x = element_blank()) +
theme(
axis.title.x = element_text(margin=margin(t=30)),
legend.title = element_text(colour = "#000000", size=12),
legend.text = element_text(colour = "#000000", size=12)
) +
guides(fill = "none") +
annotation_custom(
grid::segmentsGrob(
y0 = unit(-0.3, "npc"),
y1 = unit(-0.3, "npc"),
arrow = arrow(angle=45, length = unit(.15, 'npc')),
gp = grid::gpar(lwd=3, fill = "#000000")
),
xmin = 1,
xmax = 1700
) +
annotation_custom(
grid::segmentsGrob(
y0 = unit(-0.3, "npc"),
y1 = unit(-0.3, "npc"),
arrow = arrow(angle=45, length = unit(.15, 'npc')),
gp = grid::gpar(lwd=3, fill = "#000000")
),
xmin = 2000,
xmax = 2200
) +
annotation_custom(
grid::segmentsGrob(
y0 = unit(-0.3, "npc"),
y1 = unit(-0.3, "npc"),
arrow = arrow(angle=45, length = unit(.15, 'npc')),
gp = grid::gpar(lwd=3, fill = "#000000")
),
xmin = 2500,
xmax = 5000
)
ggsave(paste("~/Desktop/Plotv1.png", sep = ""), Plotv1, width = 8, height = 1.7)
1条答案
按热度按时间vltsax251#
问题是你指定了箭头的长度,单位是
"npc"
,也就是标准化的父坐标。因此,箭头的长度将随着父视口的绝对宽度和高度的不同而不同,即在您的例子中,每个箭头的视口是由xmin
和xmax
定义的矩形。相反,使用绝对单位表示箭头的length
。此外,我建议使用绝对单位来定位箭头。