我正在尝试将不同的范围(线)映射到图中的不同区域(见下文),geom_segment
但有些范围重叠并且根本无法显示。这是数据框的最小示例:
start = c(1, 5,8, 14)
end =c(3, 6,12, 16)
regions = c(1,2,3, 4)
regions = data_frame(regions, start, end)
from = c(1,2, 5.5, 13.5)
to = c(3,2.5,6, 15)
lines = data_frame(from, to)
我用 绘制区域,geom_rect
然后用 绘制范围(线)geom_segment
。这是情节:
plot_splice <- ggplot() +
scale_x_continuous(breaks = seq(1,16)) +
scale_y_continuous() +
geom_hline(yintercept = 1.6,
size = 20,
alpha = 0.1) +
geom_rect(
data = regions,
mapping = aes(
xmin = start,
xmax = end,
ymin = 1.5,
ymax = 1.8,
)) +
geom_segment(
data = lines,
x = (lines$from),
xend = (lines$to),
y = 1.48,
yend = 1.48,
colour = "red",
size = 3
) +
ylim(1.0, 2.2) +
xlab("") +
theme_minimal()
第一个图是使用代码生成的图,而第二个图是所需的图。
如您所见,第二行与第一行重叠,因此您根本看不到第二行。
如何更改代码以生成第二个图?
我正在尝试使用ifelse
语句,但不确定test
参数应该是什么:我希望它检查每个范围(线)是否与任何先前的范围(线)重叠以将y
位置更改为 around .05
,所以它不会重叠。
lines <- lines %>%
dplyr::arrange(desc(from))
new_y$lines = ifelse(from[1] < to[0], 1.48, 1.3)
geom_segment(
data = lines,
x = (lines$from),
xend = (lines$to),
y = new_y,
yend = new_y,
colour = "red",
size = 3
)