如何防止geom_segment中的重叠线段

问题描述 投票:0回答:1

我正在尝试使用

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
位置更改为
.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
    )

r if-statement ggplot2 data-visualization geom-segment
1个回答
1
投票

您的

geom_segment
调用不使用任何美学映射,这就是您通常如何让 ggplot 元素根据特定变量(或一组变量)更改位置。

最好在调用 ggplot 之前计算基于重叠区域数量的

geom_segment
的堆叠。这允许您将 x 和 y 值传递到美学映射中:

# First ensure that the data frame is ordered by the start time
lines <- lines[order(lines$from),]

# Now iterate through each row, calculating how many previous rows have
# earlier starts but haven't yet finished when the current row starts.
# Multiply this number by a small negative offset and add the 1.48 baseline value
lines$offset <- 1.48 - 0.03 * sapply(seq(nrow(lines)), function(i) {
   with(lines[seq(i),], length(which(from < from[i] & to > from[i])))
   })

现在做同样的情节,但在内部使用美学映射

geom_segment

 ggplot() +
  scale_x_continuous(breaks = seq(1,16), name = "") +
  scale_y_continuous(limits = c(1, 2.2), name = "") +
  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,
    mapping = aes(
      x = from, 
      xend = to, 
      y = offset, 
      yend = offset),
    colour = "red",
    size = 3
    ) +
  theme_minimal()

© www.soinside.com 2019 - 2024. All rights reserved.