我正试图使我的图形看起来尽可能好。我已经尝试了几件事。 我想使箭头穿过线的一半。而且我总是从ggplot上看到图形上的颜色看起来也更好,但是我不知道该在哪里使用。 对以下内容/建议的任何更改都非常好。
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
答案 0 :(得分:1)
您可以遵循@Axeman的建议:我的管理层非常丑陋,但我尝试使其尽可能通用,并且可以根据需要进行调整。
# 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
包,从各个方面击退了标签
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