0

I am pretty new to R and I managed to create a graph using 'plot' but I forgot error bars and I would like to add them without redoing the whole thing. Does someone know a shortcut?

I calculated the mean by hand because the data set was so small. Here is the raw data I used and the code to make the graph. I suppose I will need to have R calculate the mean and standard error, but when I try I cant get the graph to look the same.

# Pnem mean occurence each year
Plot9Pn <- c(46, 33, 28)
Plot11Pn <- c(20, 18, 10)
Plot14Pn <- c(34, 28, 26)
Plot15Pn <- c(57, 33, 12)


# Pram mean occurence each year
Plot9Pr <- c(30, 46, 95)
Plot11Pr <- c(8, 11, 14)
Plot14Pr <- c(10, 46, 46)
Plot15Pr <- c(15, 37, 110)


#hand calculated means across plots for each year- to be used in line graph
# Pn2009 <- 39.25
# Pn2010 <- 30.5
# Pn2011 <- 19

# Pr2009 <- 15.75
# Pr2010 <- 35
# Pr2011 <- 66.25


# Define 2 vectors
Pn <- c(39.25, 30.5, 19)
Pr <- c(15.75, 35, 66.25)

g_range <- range(0, Pn,Pr)

plot(Pr, type="o", pch=1, lty=1, col="red", ylim=g_range, 
 axes=FALSE, ann=FALSE)

lines(Pn, type="o", pch=2, lty=1, col="blue", ylim=g_range, 
  axes=FALSE, ann=FALSE)

# Make x axis using 2009-2011 labels
axis(1, at=1:3, lab=c("2009","2010","2011"))

# Create a title with a red, bold/italic font
title(main="Mean Yearly Pathogen Levels in Pilarcitos ", col.main="red", font.main=4)

# Label the x and y axes with dark green text

title(xlab="Year", col.lab=rgb(0,0.5,0))
title(ylab="# Positive", col.lab=rgb(0,0.5,0))

# Make y axis with horizontal labels that display ticks at 
# every 4 marks. 4*0:g_range[2] is equivalent to c(0,4,8,12).
axis(2, las=1, at=8*0:g_range[2])

# Create box around plot
box()

# Create a legend at (1, g_range[2]) that is slightly smaller 
# (cex) and uses the same line colors and points used by 
# the actual plots 

legend(1, g_range[2], c("P.ramorum","P. nemorosa"), cex=0.8, 
   col=c("red","blue"), pch=1:2, lty=1);
box()
Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
meko
  • 41
  • 1
  • 4
  • See [this](http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/) example and [this](http://stackoverflow.com/questions/15063287/add-bars-for-standard-deviation-to-a-plot-in-r) question for some clues. – Paulo E. Cardoso Aug 11 '14 at 19:40

1 Answers1

0

I think you may have miscalculated the mean of Pn2010.

But if you also hand calculate the standard deviations

Pr.sd <- c(9.946, 16.553, 44.275)
Pn.sd <- c(15.903, 7.071, 9.309

You can add error bars with

arrows(x0=1:3, y0=Pr-Pr.sd, y1=Pr+Pr.sd, 
    code=3, angle=90, length=.1, col="red")
arrows(x0=1:3, y0=Pn-Pn.sd, y1=Pn+Pn.sd, 
    code=3, angle=90, length=.1, col="blue")

Here I just add +/- 1 sd. You can calculate them however you like. You might consider offsetting the points as it doesn't look particularly nice.

The arrows function is a base graphics function so it will work with the plotting code you already had. But to make things easier in the future, you'll probably want to look into use a plotting package like ggplot2 or lattice as both of those make plotting with multiple groups much easier and ggplot makes adding error bars easier as well.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thank you MrFlick! I am in a rush to get this out and really appreciate the quick fix tip. I was able to offset the error bars for Pr, but how do I do it for the data points? I have been messing with the ylim=g_range but with no success. And thank you for pointing out that typo!!!! – meko Aug 11 '14 at 21:05
  • Instead of just using your named vector in `plot` and `lines`, I would explicitly set the x and y values separately: `plot(1:3, Pr)` for example. This will allow to you add/subtract a small offset. – MrFlick Aug 11 '14 at 21:07
  • Thanks you so much! Learning my way around slowly but surely! – meko Aug 12 '14 at 01:20