Arrow on ggplot2 mid way through path. Making plot look better

前端 未结 1 1456
花落未央
花落未央 2021-01-23 20:27

I am trying to make my graph look as good as possible. I have tried a few things. I would like to make the arrow halfway through the lines. And I always see the colours on graph

相关标签:
1条回答
  • 2021-01-23 21:10

    You can follow the @Axeman suggestion: my management is quite ugly, but I've tried to make it as general as possible, and you can tweak it as you prefere.

    # your data
    x <- c(2,3,3,4,4,5,5)
    w <- c(168,108,158,45,114,81,110)
    w <- as.data.frame(w)
    x <- as.data.frame(x)
    wts <- data.frame(x,w)
    
    library(ggplot2)
    # here you manage the coordinates of the segments
    x1 = wts[1,1]
    x2 = wts[2,1]
    x3 = wts[3,1]
    x4 = wts[4,1]
    x5 = wts[5,1]
    x6 = wts[6,1]
    x7 = wts[7,1]
    y1 = wts[1,2]
    y2 = wts[2,2]
    y3 = wts[3,2]
    y4 = wts[4,2]
    y5 = wts[5,2]
    y6 = wts[6,2]
    y7 = wts[7,2]
    
    # here you decide the colors
    col1 = "#53c68c"
    col2 = "#990000"
    
    h <- ggplot(data=wts, aes(x,w, label="w"))
    h <- h + geom_point(colour="blue")
    h <- h + labs(title="Breaches",x = "Quarter", y= "Number of Breaches")
    h <- h + theme_bw()
    h <- h +
    # now for each segment, you drawn a full segment and an halved one, to put the arrow in the middle
      geom_segment(aes(x = (x1+x2)/2, y = (y1+y2)/2, xend = x2, yend = y2, colour = col1)) +
      geom_segment(aes(x = x1, y = y1, xend = (x1+x2)/2 , yend = (y1+y2)/2, colour = col1), arrow = arrow(), show.legend=FALSE) +
    
      geom_segment(aes(x = (x2+x3)/2, y = (y2+y3)/2, xend = x3, yend = y3, colour = col2)) +
      geom_segment(aes(x = x2, y = y2, xend = (x2+x3)/2 , yend = (y2+y3)/2, colour = col2), arrow = arrow(), show.legend=FALSE) +
    
      geom_segment(aes(x = (x3+x4)/2, y = (y3+y4)/2, xend = x4, yend = y4, colour = col1)) +
      geom_segment(aes(x = x3, y = y3, xend = (x3+x4)/2 , yend = (y3+y4)/2, colour = col1), arrow = arrow(), show.legend=FALSE) +
    
      geom_segment(aes(x = (x4+x5)/2, y = (y4+y5)/2, xend = x5, yend = y5, colour = col2)) +
      geom_segment(aes(x = x4, y = y4, xend = (x4+x5)/2 , yend = (y4+y5)/2, colour = col2), arrow = arrow(), show.legend=FALSE) +
    
      geom_segment(aes(x = (x5+x6)/2, y = (y5+y6)/2, xend = x6, yend = y6, colour = col1)) +
      geom_segment(aes(x = x5, y = y5, xend = (x5+x6)/2 , yend = (y5+y6)/2, colour = col1), arrow = arrow(), show.legend=FALSE) +
    
      geom_segment(aes(x = (x6+x7)/2, y = (y6+y7)/2, xend = x7, yend = y7, colour = col2)) +
      geom_segment(aes(x = x6, y = y6, xend = (x6+x7)/2 , yend = (y6+y7)/2, colour = col2), arrow = arrow(), show.legend=FALSE)+
      scale_colour_identity()
    

    Now you can try to add the labels, I've used the nice ggrepel package, to repel them from the points

    library(ggrepel)
    txt2<- c("60","","","","","","")
    txt3<- c("","","113","","","","")
    txt4<- c("","","","","","33","")
    txt5<- c("","50","","","","","")
    txt6<- c("","","","69","","","")
    txt7<- c("","","","","","","29")
    txt7a<- c("168","108","158","45","114","81","110")
    
    # almost equal to your, but with geom_text_repel() instead of geom_text()
    h <- h + geom_text_repel(aes(label=txt2),position = position_nudge(x=0.5,y = -30))
    h <- h + geom_text_repel(aes(label=txt3),position = position_nudge(x=0.4,y = -30))
    h <- h + geom_text_repel(aes(label=txt4),position = position_nudge(x=-0.45,y = 20))
    h <- h + geom_text_repel(aes(label=txt5),position = position_nudge(x=0.1,y = 15))
    h <- h + geom_text_repel(aes(label=txt6),position = position_nudge(x=0.1,y = 30))
    h <- h + geom_text_repel(aes(label=txt7),position = position_nudge(x=0.1,y = -10))
    h <- h + geom_text_repel(aes(label=txt7a),position = position_nudge(x=0.1,y = 0))
    
    h <- h + ylim(25,170)
    h <- h + 
      scale_x_continuous(breaks=c(2,3,4,5),
                         labels=c("Q218","Q318", "Q418", "Q119"))
    
    h
    

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