Search code examples
rggplot2arc-diagram

geom_curve - plot all curves above zero


I am trying to make an arc plot similar to arcdiagram package, using ggplot2's 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')

enter image description here

Some curves go below 0, how can I make them all to go above Y = 0?

Data:

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

Solution

  • Based on curvature the curve will either bend left or right. This means in this case, that when the curve is drawn from A to B, and curvature is 1 it will have a right-hand bend. Therefore if A smaller than B, the curve will be 'negative' (below the y-axis) and if A is larger than B it will be positive.

    One way to get all curves on the same side is with two geom_curves, one for each situation:

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

    Or, we can swap the pairs around when they are aligned the wrong way:

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

    Both result in:

    enter image description here