r ggplot2 annotate pointrange and segment in single facet -


i creating 3 facet plot ggplot following data

library(ggplot2)  test<-as.data.frame(c(1,2,3,4,1,2,3,4,1,2,3,4)) colnames(test)<-"e12" test$e23<-c(nan,nan,nan,nan,2,3,4,5,2,3,4,5) test$e34<-c(nan,nan,nan,nan,nan,nan,nan,nan,3,4,5,6) test$age<-c(1,2,3,4,1,2,3,4,1,2,3,4) test$facet<-c(1,1,1,1,2,2,2,2,3,3,3,3)  ggplot(test, aes(x=age)) +    facet_grid( ~ facet) +   geom_line(aes(y = e12), size=0.25, colour="red", linetype="dashed") +   geom_point(aes(y = e12), size=2.5, shape=21, fill="red", colour=na) +   geom_line(aes(y = e23), size=0.25, colour="blue", linetype="dashed") +   geom_point(aes(y = e23), size=2.5, shape=21, fill="blue", colour=na) +   geom_line(aes(y = e34), size=0.25, colour="green", linetype="dashed") +   geom_point(aes(y = e34), size=2.5, shape=21, fill="green", colour=na) +   annotate("pointrange", x=3.05, y=6.1, ymin=6.1, ymax=6.1, colour="red", size=0.5) +   annotate("segment", x=2.8, xend=3.4, y=6.1, yend=6.1, colour="red", size=0.5, linetype="dashed") +   annotate("text", x=3.6, y=6.1, parse=t, label="e[1%;%2]", size=3.1, family="calibri", colour="black") +   theme(strip.text = element_text(face="bold", size=rel(1)),         strip.background = element_rect(fill="white", colour="white", size=1)) +   scale_x_continuous(limits=c(0.5,4.5), breaks=seq(1, 4, 1), minor_breaks=seq(1, 4, 1), expand = c(0, 0)) +    scale_y_continuous(limits=c(0,6.5), breaks=seq(0, 6.5, 1), minor_breaks=seq(0,6.5,0.2), expand = c(0, 0)) 

however, poses 2 problems:

  1. i annotations (pointrange, segment , text) appear in 1 facet. have seen how create dataframe annotate text in single facet, know if possible segments , pointranges.

  2. the text should read e1;2 1;2 in subscript - although putting ";" in plotmath causes errors.

enter image description here

to produce plot want might difficult, not mess around details of ggplot. can show how produce following plot:

way using gpgplot not how (as far understand) intended used. writing command each line want plot. idea of ggplot put data data frame , map columns of data frame graphical elements. might find following resources helpful: ggplot2 page , cookbook r, find tons of examples ggplot2.

the first step prepare data such plot can done. assuming test defined in example, following code format data appropriately:

library(reshape2) plot.test <- melt(test,id=c("age","facet")) plot.test <- subset(plot.test,!is.na(value)) 

look @ plot.test , documentation of melt understand step better. first line puts data in different shape. instead of 1 column per curve (e_12,e_23,e_34), have 1 column values , column indicates, curve value belongs. second line throws away undefined values, not needed.

now data ready, plot can produced follows:

e.labs <- c(expression(e["1;2"]),expression(e["2;3"]),expression(e["3;4"]))  ggplot(plot.test,aes(x=age,y=value,colour=variable)) +   facet_grid(.~facet) +   geom_line(size=0.25, linetype="dashed") +    geom_point(size=2.5) +    theme(strip.text = element_text(face="bold", size=rel(1)),         strip.background = element_rect(fill="white", colour="white", size=1),         legend.position=c(0.93,0.15)) +   scale_colour_discrete(labels=e.labs) 

as see, geom_line , geom_point both need called once. mapping of data done inside ggplot command. says variable age should used x-axis, , variable value (which contains data columns e_12 etc. original data frame) should used y-axis. column variable, states whether data comes e_12, e_23, or e_34 column, used set different colours. since theses mappings same points , lines, can defined globally in ggplot() instead of inside geom_ functions.

if use ggplot correctly, assigning columns graphical objects, legends produced automatically. code contains 2 statements customize legend: scale_colour_discrete used change labels in legend, legend.position inside theme used move legend on plot requested.

if wanted replace y-axis labels , legend title meaningful, can use labs function. add following plot

+ labs(y="my y label",colour="my legend title") 

i have not bothered include customisation of x- , y-axis, seems know how this.