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

4

1 回答 1

1

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

geom_segment最好在调用 ggplot 之前计算基于重叠区域数量的堆叠。这允许您将 x 和 y 值传递到美学映射中:

# First ensure that the data feame 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()

在此处输入图像描述

于 2020-11-15T21:17:54.707 回答