1

I have created a plot using ggplot2 package in R using the following data:

# Ind filt objs        flux  filt    objs err_flux#

01   4590 obj1 1005.448892  4590 errobj1   0.0401  
02   6220 obj1 1420.626789  6220 errobj1   0.0392  
03   7640 obj1 1855.581355  7640 errobj1   0.0432  
 *.......skipped lines...*    
20 21512 obj2  949.642188 21512 errobj2     0.1516    
21  2030 obj2    9.838299  2030 errobj2   **0.0000**  
22  2253 obj2   17.097003  2253 errobj2   **0.0000**  
23  2612 obj2   14.754347  2612 errobj2   **0.0000**  
24  3470 obj2   14.890868  3470 errobj2   **0.0000**  

I am plotting flux vs filt with ggplot and then adding the y-error bars as defined in the err_flux column. I would like to draw arrows from the data points for which the err_flux==0..

Here is my program so far:

g <- ggplot(finaldata[1:26, ], aes(x = filt, y = log10(flux), color = objs))
gf1 <- g + geom_point(size = 2, alpha = 0.8, pch = 15) + theme_bw()
gf <- gf1 + geom_errorbar(aes(ymax = log10(flux) + err_flux,
                              ymin = log10(flux) - err_flux),
                           size = 0.5, color = "darkgrey")
gff <-gf + xlab("wavelength") + ylim(0.3,4.5) +
          geom_smooth(method = "loess", se = FALSE)
gfinal <- gff + scale_color_hue(l = 10, c = 75)

The resulting plot is shown below : enter image description here

Here is what I have tried to add arrows (e.g. the 4 leftmost points for 'obj2' in the plot shown above, but it does not work:

for (i in 1:nrow(finaldata)) {
 if (finaldata$err_flux==0.){
   gfinal<-gfinal + geom_segment(x=finaldata$filt[i], y=finaldata$flux[i], xend=finaldata$filt[i], yend=finaldata$flux[i]-0.5,arrow=arrow(length=unit(0.25,"cm")))
  }
 }

Moreover, if I apply 'geom_segment' to the subset of my actual data, it won't add to my existing plot (gfinal), due to different dimensions.

Any suggestions/help would be appreciated.

Cactus
  • 27,075
  • 9
  • 69
  • 149
akaur
  • 389
  • 1
  • 6
  • 22
  • Use `annotate`, and don't use a `for` loop. Subset the data down to what you want annotated and use the `data` argument. – Gregor Thomas Oct 13 '15 at 00:12
  • @Gregor : This is the first time I am trying to work with R for plotting, so this is what I tried : `sub<-subset(finaldata[1:26,],finaldata$err_flux==0.) gfinal+annotate(sub,x=sub$filt,xend=sub$filt,y=sub$flux,yend=sub$flux-0.5,color="red")` – akaur Oct 13 '15 at 00:25
  • 1
    nevermind ! I needed to define this as follows: `annotate("segment",x=sub$filt,xend=sub$filt,y=log10(sub$flux),yend=log10(sub$flux)-0.5,color="red")` – akaur Oct 13 '15 at 00:37
  • If you'd like, answering your own question is encouraged when you work out the solution yourself. – Gregor Thomas Oct 13 '15 at 03:58

1 Answers1

2

Annotate function helped me to draw downwards pointing arrows (upper bounds) from selected data points.

I replace the last part of my program, which used a for and a if loop with geom_segment to annotate as follows :

sub<-subset(finaldata[1:26,],finaldata[1:26,]$err_flux==0.)
gfinal+annotate("segment",x=sub$filt,xend=sub$filt,y=log10(sub$flux),yend=log10(sub$flux)-0.25,color="red",arrow = arrow(length = unit(0.2, "cm")))

which resulted in the following figure :See 4 <code>red</code> arrows on 4 leftmost data points for "obj2"

akaur
  • 389
  • 1
  • 6
  • 22