geom_curve - 绘制零以上的所有曲线

时间:2016-10-14 09:41:21

标签: r ggplot2 arc-diagram

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

library(ggplot2)

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

enter image description here

有些曲线低于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))

1 个答案:

答案 0 :(得分:3)

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

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

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')

两者都导致:

enter image description here