ggplot2 polar plot arrows

后端 未结 1 1472
猫巷女王i
猫巷女王i 2020-12-06 03:18

I can use ggplot2 easily to draw a graph like below:

\"1\"

In fact, for my data, it is like below:

相关标签:
1条回答
  • 2020-12-06 03:56

    Set up data (from dput):

    polar <- structure(list(degree = c(120L, 30L, -120L, 60L, 150L, -90L, 
    -60L, 0L), value = c(0.5, 0.2, 0.2, 0.5, 0.4, 0.14, 0.5, 0.6)), .Names = c("degree", 
    "value"), class = "data.frame", row.names = c(NA, -8L))
    

    You can get the straight lines fairly easily -- you just have to make sure your segments start at degree rather than 0:

    library(ggplot2)
    base <- ggplot(polar, aes(x=degree, y=value))
    p <- base + coord_polar()
    p+ geom_segment(aes(y=0, xend=degree, yend=value))
    

    enter image description here Adding arrows, however, makes it look like there may be a bug (?) -- the coordinate transform doesn't get taken into account in computing the angle of the arrows:

    library(grid)
    p+ geom_segment(aes(y=0, xend=degree, yend=value) ,
                    arrow=arrow(length=unit(0.3,"cm")))
    

    enter image description here You can (sort of) hack around this by drawing your own arrowheads:

    awid <- 2
    p + geom_segment(aes(y=0, xend=degree, yend=value))+
        geom_segment(aes(y=value-0.05,yend=value,x=degree-awid/value,xend=degree))+
        geom_segment(aes(y=value-0.05,yend=value,x=degree+awid/value,xend=degree))
    

    enter image description here

    If you look closely, you can see that the arrowheads aren't perfectly straight (the effect is much more obvious if you make awid larger).

    0 讨论(0)
提交回复
热议问题