ggplot2中途通过路径的箭头.使情节看起来更好 [英] Arrow on ggplot2 mid way through path. Making plot look better

查看:52
本文介绍了ggplot2中途通过路径的箭头.使情节看起来更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使我的图形看起来尽可能好.我已经尝试了几件事. 我想使箭头穿过线的一半.而且我总是从ggplot上看到图表上的颜色看起来也更好,但是我不知道该从哪里去. 对以下内容/建议的任何更改都将非常棒.

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 graphs from ggplot look nicer too but I don't know where to go with it. Any changes to the below/advice would be great.

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)


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<-h +
  geom_line(data=wts[c(1,2),], colour="green",arrow=arrow())+
  geom_line(data=wts[c(3,4),], color="green",arrow=arrow())+ 
  geom_line(data=wts[c(5,6),], color="green",arrow=arrow())+
  geom_line(data=wts[c(2,3),], color="red",arrow=arrow())+
  geom_line(data=wts[c(4,5),], color="red",arrow=arrow())+
  geom_line(data=wts[c(6,7),], color="red",arrow=arrow())

# h<-h+
#   geom_point(data=wts[c(8,9,10),], color="red")
h



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

h<-h+ geom_text(aes(label=txt2),position = position_nudge(x=0.5,y = -30))
h<-h+ geom_text(aes(label=txt3),position = position_nudge(x=0.4,y = -30))
h<-h+ geom_text(aes(label=txt4),position = position_nudge(x=-0.45,y = 20))
h<-h+ geom_text(aes(label=txt5),position = position_nudge(x=0.1,y = 15))
h<-h+ geom_text(aes(label=txt6),position = position_nudge(x=0.1,y = 30))
h<-h+ geom_text(aes(label=txt7),position = position_nudge(x=0.1,y = -10))
h<-h+ geom_text(aes(label=txt7),position = position_nudge(x=0.1,y = -10))
h<-h+ geom_text(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

推荐答案

您可以遵循@Axeman的建议:我的管理非常丑陋,但是我尝试使其尽可能通用,您可以对其进行调整您更喜欢.

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

现在您可以尝试添加标签了,我已经使用了不错的ggrepel包,从各方面击退了标签

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

这篇关于ggplot2中途通过路径的箭头.使情节看起来更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆