Hi there Stack Overflow,
TL;DR: I want to plot a bar plot with bar height 34.30, the upper error
bar extending to 55.68, and the lower error bar extending to 21.12. Can I set
error bars manually in R?
Longer version:
I am doing a delta delta Ct calculation for gene expression data. I want to use a bar plot to show my expression. I also want to propagate error through my calculations. I can do the propagation calcs in R or excel following the methodology of Livak et al. (2001), and get my upper and lower bounds for my confidence intervals. But to plot these in R is troublesome, because I first take an average of each of my treatments, then take the difference between those averages, then transform the difference. Thus, the value I'm plotting doesn't get an input from multiple measurements, but just a single number. So plotting error bars the standard way doesn't work, because there is no Margin of Error for one number.
Doing the calcs myself, I can find the upper and lower bounds of the 95% confidence intervals. I'd like to be able to just specify these as the upper and lower limits of my error bars. Is this possible?
Livak et al. (2001): http://www.gene-quantification.net/livak-2001.pdf
Here's an example of what the data look like:
control
sample, gene1, gene2
1 , 30.00, 27.00
2 , 30.50, 27.25
3 , 29.50, 26.50
4 , 30.10, 26.90
treatment
sample, gene1, gene2
5 , 25.00, 27.00
6 , 25.50, 27.15
7 , 24.50, 26.80
8 , 25.10, 27.10
So then the averages of each gene in the control are: gene1 = 30.03, gene2 = 26.91
The difference of control values is then: 30.03-26.91 = 3.12
And then the averages of each gene in the treatment are: gene1 = 25.03, gene2 = 27.01
The difference of control values is then: 25.03-27.01 = -1.98
and
The difference in expression between the control and treatment is: -1.98 - 3.12 = -5.10
The expression value I'm plotting (fold change) is: 2^-(5.10) = 34.30 So the bar represents a 34.30 fold change in expression.
I'm using margin of error for my error bars, and I'm getting the upper and lower bounds of (-5.10) and then transforming them in the same fashion (i.e. 2^-X) to find my upper and lower bounds of the transformed data's margin of error. I need to calc the upper and lower bounds before transformation, otherwise I get the wrong upper and lower bounds on my fold change in expression.
Margin of Error = (StDev*(T-stat/sqrt(n))) = 0.70, thus
The upper bound of -5.10 is -4.40
The lower bound of -5.10 is -5.80
The transformed gene expression (2^-(-5.1)) is: 34.30
The transformed upper bound is: 55.68
The transformed lower bound is: 21.12
So I would want my bar to be 34.30 high, and then the upper error bar to extend to 55.68 and the lower error bar to extend to 21.12.
Thanks!
Yup, so I'm fairly confident this is possible. I'm assuming that you can get your upper and lower bounds as two vectors of values, call these upper
and lower
. First define an error bar function:
# Adding error bars to the barplot
error.bar <- function(x, y, upper, lower=upper, length=0.1, ...){
if(length(x) != length(y) | length(y) !=length(lower) | length(lower) != length(upper))
stop("vectors must be same length")
arrows(x,y+upper, x, y-lower, angle=90, code=3, length=length, lwd = 2, ...)
}
Then make your plot with whatever values you want.
# Plotting the barplot
barx <- barplot(values)
Now we can put the error plots on, replacing y_values
, lower_values
and upper_values
with your data or what you calculated.
# Adding the bars.
error.bar(barx, y_values, lower_value, upper_value)