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 :
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.