在只有某些方面ggplot添加注解(段/箭头)[重复](Adding annotation (seg

2019-07-18 15:53发布

可能重复:
在GGPLOT2个别小注释文本

我工作的一个数据集有3种。 他们是苹果,橘子和香蕉。

我想注释只有底部面板上的小面。 然而,在默认情况下,我对所有的地块越来越注释。 我能只在所希望的描绘让文本注释。 不过,我很困惑什么,我需要的箭头/段做。

这里是我的代码:

library(ggplot2)
library(grid)

tempobs <- read.csv("temp data share.csv",header=T, sep=",")
p1 <- ggplot(tempobs,aes(x=time,y=data))+geom_point(data=tempobs,aes(x=time,y=data,colour=group1))+facet_wrap(~id,ncol=1)+theme_bw()
p1 <- p1 + xlab("Julian Day (2008-2009)")+ylab(expression(Temperature~(degree*C)))+ element_blank()+ theme(
    legend.position="right",
    legend.direction="vertical",
    legend.title = element_blank()) +coord_cartesian(xlim=c(250,550))+coord_cartesian(ylim=c(0,40))+scale_x_continuous(breaks=c(250,300,350,400,450,500,550),labels=c("250","300","350","34","84","134","184"))
p1

### This is how it should look like (though shows annotations for all the plots)
p + annotate("text",x=340,y=3,label="2008",size=3)+annotate("segment",x=366,xend=366,y=0,yend=2,size=0.5)+annotate("text",x=390,y=3,label="2009",size=3)+annotate("segment",x=366,xend=310,y=1,yend=1,size=0.5,arrow=arrow(length=unit(0.2,"cm")))+annotate("segment",x=366,xend=420,y=1,yend=1,size=0.5,arrow=arrow(length=unit(0.2,"cm")))


### This is what I did to show text annotation on the bottom panel
ann_text <- data.frame(x=c(340,390),y=c(3,3),id=c("orange"),label=c("2008","2009"))
p1 <- p1 + geom_text(data=ann_text,aes(x=x,y=y,label=label,size=3),show_guide=F)
p1

现在,我想添加基于整体图形箭头和段。

我的数据上可以找到https://www.dropbox.com/s/dfcmqrslskwdh80/temp%20data%20share.csv

我的输出

这是我仅有的文字注释了。 但对于部分注释我总是得到错误。 你可以注意到文本标签2008年和2009年增加了底部面板上。

输出显示我想要的注解,但它是在所有的方面。 我想只在一个底部。

非常感谢。

问候,Jdbaba

Answer 1:

你应该做一个数据帧(相同的文本标签),还为您要使用的段值。

ann_line<-data.frame(xmid=366,xmin=310,xmax=420,y0=0,y2=2,y=1,
    id=factor("orange",levels=c("apple","banana","orange")))

然后使用geom_segment()来绘制所有元素

p1 + geom_segment(data=ann_line,aes(x=xmid,xend=xmin,y=y,yend=y),arrow=arrow(length=unit(0.2,"cm")),show_guide=F)+
     geom_segment(data=ann_line,aes(x=xmid,xend=xmax,y=y,yend=y),arrow=arrow(length=unit(0.2,"cm")),show_guide=F)+
     geom_segment(data=ann_line,aes(x=xmid,xend=xmid,y=y0,yend=y2),show_guide=F)+   
     geom_text(data=ann_text,aes(x=x,y=y,label=label,size=3),show_guide=F)



文章来源: Adding annotation (segment / arrow) in only certain facet ggplot [duplicate]
标签: r ggplot2