Look of arrows in ggplot2 geom_segment()

前端 未结 2 556
时光取名叫无心
时光取名叫无心 2020-12-29 21:32

I\'m trying to make a plot with arrows in ggplot2 looking something like this, which was made using base R grapics. (colors are not important)

相关标签:
2条回答
  • 2020-12-29 22:08

    The challenge seems to be that the arrow constructor from the grid package gets messed up if size is invoked in the geom_segment block.

    so

    p <- ggplot(df3) + coord_flip()
    
    p1 <- p + geom_bar(aes(x=group,y=max(c(value1,value2))*1.1),width=0.2, stat="identity",position="identity",alpha=0.2)
    
    df1<-filter(df3,time=="1999")
    
    p1 + geom_segment(data=df1,aes(x=group,xend=group,y=value1,yend=value2),color="blue",size=8,arrow=arrow(angle=20,type="closed",ends="last",length=unit(1,"cm")))
    

    looks ridiculous as you show. I tried the workaround of of separating the segment into just a fat segment and an arrow on a skinny segment (two layers) like so:

    p2<-p1 + geom_segment(data=df1,aes(x=group,xend=group,y=value1,yend=value2), color="blue",arrow=arrow(angle=20,type="closed",ends="last",length=unit(1,"cm")))
    
    p2 + geom_segment(data=df1,aes(x=group,xend=group,y=value1,yend=value2), color="blue",size=8)
    

    but now the fat segment end is not mitred and so obscures the arrow.

    Fixing the arrow parameter seems to be needed.

    0 讨论(0)
  • 2020-12-29 22:17

    update: ggplot2 v2.1.0.9001

    If the plot is in your current window you can edit the shape of the arrow directly with

    grid.force()
    # change shape of arrows
    grid.gedit("segments", gp=gpar(linejoin ='mitre'))
    # change the shape in legend also
    grid.gedit("layout", gp=gpar(linejoin ='mitre'))
    

    If the plot is in your current window you can edit the shape of the arrow directly with

    grid.gedit("segments", gp=gpar(linejoin ='mitre'))
    

    ggplot now seems to have changed the legend key to an arrow shape, so if you want to change the shape of these as well, you can do this across the full plot with

    grid.gedit("gTableParent", gp=gpar(linejoin ='mitre'))
    

    original answer

    Not less hacky, but perhaps easier?? You can edit the grobs returned by ggplotGrob.

    If p is your plot:

    g <-  ggplotGrob(p)
    
    idx <- grep("panel", g$layout$name)
    
    nms <- sapply(g$grobs[[idx]]$children[[3]]$children , '[[', "name")
    
    for(i in nms) {
        g$grobs[[idx]]$children[[3]] <- 
                  editGrob(g$grobs[[idx]]$children[[3]], nms[i], 
                            gp=gpar(linejoin ='mitre'), grep=TRUE)
    }
    
    grid.newpage()
    grid.draw(g)
    

    enter image description here

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