4

我正在尝试使用 ggplot2 制作类似于arcdiagram包的弧线图geom_curve

library(ggplot2)

ggplot(myDat, aes(x = A, xend = B, y = 0, yend = 0)) +
  geom_curve(alpha = 0.2, curvature = 1, ncp = 1000, lineend = 'butt')

在此处输入图像描述

有些曲线低于 0,我怎样才能让它们都高于 Y = 0?

数据:

myDat <- 
  structure(list(A = c(36047276L, 36074979L, 36074979L, 36074979L, 
                       36074979L, 36074979L, 36074979L, 36074979L, 36074979L, 36074979L, 
                       36074979L, 36077099L, 36077099L, 36077099L, 36077099L, 36077099L, 
                       36077099L, 36077099L, 36077099L, 36098040L, 36098040L, 36098040L, 
                       36098040L, 36098040L, 36098040L, 36098040L, 36098040L, 36098040L, 
                       36098040L, 36098040L, 36098040L, 36098040L, 36098040L, 36098040L, 
                       36098040L, 36098040L, 36099840L, 36099840L, 36099840L, 36099840L, 
                       36099840L, 36099840L, 36099840L, 36099840L, 36099840L, 36099840L, 
                       36099840L, 36099840L, 36099840L, 36099840L, 36099840L, 36099840L, 
                       36099840L, 36101586L, 36101586L, 36101586L, 36101586L, 36101586L, 
                       36101586L, 36101586L, 36101586L, 36101586L, 36101586L, 36101586L, 
                       36101586L, 36101586L, 36101586L, 36101586L, 36101586L, 36101586L, 
                       36103565L, 36103565L, 36103565L, 36103565L, 36103565L, 36103565L, 
                       36103565L, 36103565L, 36103565L, 36103565L, 36103565L, 36103565L, 
                       36103565L, 36103565L, 36103565L, 36103565L, 36103565L),
                 B = c(36047417L, 
                       36077099L, 36077279L, 36077863L, 36078510L, 36079111L, 36079565L, 
                       36080165L, 36080428L, 36082473L, 36082907L, 36074979L, 36077279L, 
                       36077863L, 36078510L, 36079111L, 36079565L, 36080165L, 36082907L, 
                       36096300L, 36097775L, 36098987L, 36099840L, 36099952L, 36100767L, 
                       36101156L, 36101586L, 36101633L, 36101926L, 36102035L, 36102381L, 
                       36102833L, 36103149L, 36103565L, 36103872L, 36104121L, 36096300L, 
                       36097775L, 36098040L, 36098987L, 36099952L, 36100767L, 36101156L, 
                       36101586L, 36101633L, 36101926L, 36102035L, 36102381L, 36102833L, 
                       36103149L, 36103565L, 36103872L, 36104121L, 36096300L, 36097775L, 
                       36098040L, 36098987L, 36099840L, 36099952L, 36100767L, 36101156L, 
                       36101633L, 36101926L, 36102035L, 36102381L, 36102833L, 36103149L, 
                       36103565L, 36103872L, 36104121L, 36096300L, 36097775L, 36098040L, 
                       36098987L, 36099840L, 36099952L, 36100767L, 36101156L, 36101586L, 
                       36101633L, 36101926L, 36102035L, 36102381L, 36102833L, 36103149L, 
                       36103872L, 36104121L)), .Names = c("A", "B"),
            class = "data.frame", row.names = c(NA, -87L))
4

1 回答 1

4

基于curvature曲线将向左或向右弯曲。这意味着在这种情况下,当曲线从 A 绘制到 B 并且曲率为 1 时,它将向右弯曲。因此,如果 A 小于 B,曲线将为“负”(在 y 轴下方),如果 A 大于 B,则曲线为正。

使所有曲线在同一侧的一种方法是使用两个geom_curves,每种情况一个:

ggplot(mapping = aes(x = A, xend = B, y = 0, yend = 0)) +
  geom_curve(data = subset(myDat, A > B),
             alpha = 0.2, curvature = 1, ncp = 1000, lineend = 'butt') +
  geom_curve(data = subset(myDat, A < B),
             alpha = 0.2, curvature = -1, ncp = 1000, lineend = 'butt')

或者,当它们以错误的方式对齐时,我们可以交换它们:

myDat2 <- myDat
myDat2$A <- pmax(myDat$A, myDat$B) 
myDat2$B <- pmin(myDat$A, myDat$B)

ggplot(myDat2, aes(x = A, xend = B, y = 0, yend = 0)) +
  geom_curve(alpha = 0.2, curvature = 1, ncp = 1000, lineend = 'butt')

两者都导致:

在此处输入图像描述

于 2016-10-14T09:51:09.767 回答