I would really appreciate the assistance of the helpful community in this forum with a little issue I'm having with R, and the arrow function when trying to draw error bars. I chose to use the arrows function to generate my error bars for their simplicity in adding to the graphs I've already created.
I've been successful in drawing two scatterplots for the following table using plot()
X sitting.time exercise.time se.sit.time se.ex.time
1 535.7308 70.08000 45.39388 13.23958
2 512.8800 65.80000 50.92198 13.98714
3 500.2727 80.09091 47.00300 13.90255
4 504.4762 77.35000 48.38485 17.22151
5 487.0588 84.00000 51.62004 14.62120
6 487.3333 83.53333 51.15082 17.52736
7 462.5294 83.52941 49.97000 17.61185
8 458.1111 71.78947 47.30922 12.64716
9 471.9333 73.66667 57.13613 16.97271
10 517.1333 84.92857 62.34817 18.61450
11 526.5625 73.93750 50.25767 17.67990
12 484.0000 96.22222 73.67854 23.97266
In the table, sitting.time
and exercise.time
are my y-values, X
are x-values, and se.sit.time
and se.ex.time
are standard errors.
The commands I've successfully used to generate two plots (including legend) are as follows:
plot(weekly.times$X,weekly.times$sitting.time,
xlim=c(1,12),ylim=c(0,600),
xaxt="n", yaxt="n",xlab="Week",
ylab="Time (minutes)", type="p",col="red")
axis(side=1,at=c(1:12))
axis(side=2,at=seq(0,600,by=50))
lines(weekly.times$X,weekly.times$sitting.time,
xlab="Week", ylab="Time", col="red")
par(new=TRUE)
plot(weekly.times$X,weekly.times$exercise.time,
xlab='', ylab='', axes=FALSE, col="blue")
lines(weekly.times$X,weekly.times$exercise.time,
xlab='', ylab='', col="blue")
legend("bottom",inset=c(-0.3,0),
legend=c("Sitting Time", "Exercise Time"),
col=c("red", "blue"), lty=1:2, cex=0.8)
However, after trying to add the arrows()
after the first plot, I do not see any error bars generated in the same way as the original answer to a similar post on the topic: Scatter plot with error bars
arrows(weekly.times$sitting.time, sitCI.down,
weekly.times$sitting.time, sitCI.up, length=0.05, angle=90, code=3)
My output for the first plot, where I would like to add error bars. I've also run the arrows function after this plot:
My output with both plots in a graph. I would eventually like both graphs to have error bars. I've also tried to run arrows() here without avail:
Does anyone know what I might be doing wrong, and why the error plots are not showing up in the graphic?
After following @rbatt 's solution, I got the graph to work with error plots as below:
I used the following code:
p1.ylim <- range(c(weekly.times$sitting.time, sitCI.down, sitCI.up))
p1.xlim <- range(weekly.times$X)
par(mar=c(4,4,0.5,4))
plot(weekly.times$X, weekly.times$sitting.time, xlim=p1.xlim, ylim=p1.ylim, ylab="Sitting Time (minutes)", xlab="Week", type="o",col="red", xaxt="n", yaxt="n")
axis(side=1,at=weekly.times$X)
axis(side=2,at=seq(p1.ylim[1],p1.ylim[2],by=50))
red2 <- adjustcolor("red", alpha.f=0.25)
arrows(weekly.times$X, sitCI.down, weekly.times$X, sitCI.up, length=0.05, angle=90, code=3, col=red2)
par(new=TRUE)
plot(weekly.times$X,weekly.times$exercise.time, xlab='', ylab='', axes=FALSE, col="blue", type="o")
axis(side=4)
blue2 <- adjustcolor("blue", alpha.f=0.25)
arrows(weekly.times$X, exCI.down, weekly.times$X, exCI.up, length=0.05, angle=90, code=3, col=blue2)
mtext("Exercise Time (minutes)", side=4, line=3)
legend("bottom",inset=c(-0.3,0), legend=c("Sitting Time", "Exercise Time"), col=c("red", "blue"), lty=1:2, cex=0.8)
However, I now have new data as follows, which does not get the error bars to fit.
week sitting.time exercise.time se.sit.time se.ex.time
1 1 3643.087 471.5833 349.5165 98.06667
2 2 3516.478 472.7917 337.8359 99.83149
3 3 3373.000 535.6667 344.9220 103.70350
4 4 3480.952 510.2857 352.2140 118.32880
5 5 3235.882 571.3529 345.0725 104.53620
6 6 3359.000 587.1333 349.3253 122.10290
7 7 3246.250 554.1250 358.0210 131.71030
8 8 3173.889 502.7895 326.9745 88.58391
9 9 3438.643 513.0667 354.2999 119.08770
10 10 3581.154 807.7143 323.0524 230.90890
11 11 3661.250 693.7059 357.1707 212.60630
12 12 3297.500 909.0000 589.6690 276.65540
And using the same code I get the following plot:
Do you know where in the axis code I went wrong which causes the error bars for the blue line to not show?
Thank you.