箭头函数不适用于R中的错误条

箭头函数不适用于R中的错误条,r,plot,R,Plot,我真的非常感谢这个论坛中有帮助的社区的帮助,我有一个关于R的小问题,以及在尝试绘制错误条时箭头函数。我选择使用arrows函数来生成错误条,因为它们可以简单地添加到我已经创建的图形中 我已经成功地使用plot() 在表中,siting.time和exercise.time是我的y值,X是X值,se.sit.time和se.ex.time是标准错误 我已成功使用以下命令生成两个绘图(包括图例): plot(weekly.times$X,weekly.times$sitting.time, xli

我真的非常感谢这个论坛中有帮助的社区的帮助,我有一个关于R的小问题,以及在尝试绘制错误条时箭头函数。我选择使用arrows函数来生成错误条,因为它们可以简单地添加到我已经创建的图形中

我已经成功地使用
plot()

在表中,
siting.time
exercise.time
是我的y值,
X
是X值,
se.sit.time
se.ex.time
是标准错误

我已成功使用以下命令生成两个绘图(包括图例):

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)
但是,在尝试在第一个绘图之后添加
箭头()
之后,我没有看到任何错误条以与该主题类似帖子的原始答案相同的方式生成:

第一个绘图的输出,我想在其中添加错误条。在此绘图之后,我还运行了箭头函数:

我的两个图形的输出。我最终希望两个图都有误差条。我还尝试在此处运行arrows(),但没有成功:

是否有人知道我可能做错了什么,以及为什么错误图没有显示在图形中


在遵循@rbatt的解决方案后,我得到了如下用于错误图的图形:

我使用了以下代码:

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)
使用相同的代码,我得到了以下曲线图:

你知道我在轴代码中哪里出错了吗,这导致蓝线的错误条不显示


谢谢。

您有两个主要问题。首先,您为
arrows()的
x0
x1
参数提供了一个y轴变量
weekly.times$siting.time
;我已将其更改为每周。乘以$X,这是一周,在X轴上。第二,您尝试在调用par(new=TRUE)和添加锻炼时间后添加箭头,锻炼时间的范围在~60到~100之间,并且由于未设置
ylim
,并且
箭头在500-600范围内,因此它们会在远离绘图的地方创建(即使第一个问题已经解决)

下面是对原始代码进行最小更改以生成合适图形的代码。请注意,您没有提供对象
sitCI.down
sitCI.up
的数据,所以我只做了坐姿时间的±10%。另外,我首先将
dput()
的输出包含在一个data.frame上,该data.frame是我从您的问题中手动复制的;将来,如果您在数据上使用
dput()
,并将输出复制到问题中,则可以更轻松地再现结果

使用dput的输出创建数据集
weekly.times在您的数据上使用
dput()
,这样我们就可以重新创建您现在看到的图表。我刚刚添加了图像的链接-抱歉,我还没有足够的信誉点来嵌入它们。我刚刚嵌入了它们,它们将在编辑后显示谢谢,C8H10N4O2:)@rbatt Hi。虽然您的解决方案非常完美,但我遇到了一个新数据和绘制错误条的问题,这可能与新轴有关。我已经玩弄了这些代码,但没有用。任何帮助都将不胜感激。这是完美的,非常感谢您清晰的演练!Hi@rbatt我有了新的数据,这些数据现在不再适用于解决方案-我的错误条在第二张图中不再显示。我意识到这与我的y轴范围有关,但在尝试了我能想到的所有解决方案后,我无法得到显示的误差条。我在原来的问题下面加了我的问题。任何帮助都将不胜感激!谢谢
arrows(weekly.times$sitting.time, sitCI.down, 
  weekly.times$sitting.time, sitCI.up, length=0.05, angle=90, code=3)
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)
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
weekly.times <- structure(list(X = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), 
    sitting.time = c(535.7308, 512.88, 500.2727, 504.4762, 487.0588, 
    487.3333, 462.5294, 458.1111, 471.9333, 517.1333, 526.5625, 
    484), exercise.time = c(70.08, 65.8, 80.09091, 77.35, 84, 
    83.53333, 83.52941, 71.78947, 73.66667, 84.92857, 73.9375, 
    96.22222), se.sit.time = c(45.39388, 50.92198, 47.003, 48.38485, 
    51.62004, 51.15082, 49.97, 47.30922, 57.13613, 62.34817, 
    50.25767, 73.67854), se.ex.time = c(13.23958, 13.98714, 13.90255, 
    17.22151, 14.6212, 17.52736, 17.61185, 12.64716, 16.97271, 
    18.6145, 17.6799, 23.97266)), .Names = c("X", "sitting.time", 
"exercise.time", "se.sit.time", "se.ex.time"), row.names = c(NA, 
-12L), class = "data.frame")
plot(weekly.times$X, weekly.times$sitting.time, xlim=c(1,12), ylim=c(0,600), 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")

sitCI.down <- weekly.times$sitting.time + c(-0.1)*weekly.times$sitting.time
sitCI.up <-  weekly.times$sitting.time + c(0.1)*weekly.times$sitting.time
arrows(weekly.times$X, sitCI.down, weekly.times$X, sitCI.up, length=0.05, angle=90, code=3)

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)
# Calculate CI's first to account for them in ylim of plot
sitCI.down <- weekly.times$sitting.time + c(-0.1)*weekly.times$sitting.time
sitCI.up <-  weekly.times$sitting.time + c(0.1)*weekly.times$sitting.time

# I very frequently follow this idiom for making sure the ylims are set properly
# It is better to define ylims programmatically than by using magic numbers like c(0,600)
p1.ylim <- range(c(weekly.times$sitting.time, sitCI.down, sitCI.up))
p1.xlim <- range(weekly.times$X) # same idea for xlim, but simpler case

# When creating your plot, you can set type="o" do draw the points and the lines at the same time
# Also, if you're going to create the axes with axis(), you can also not draw them in the plot
# Turn axes off using xaxt="n" and yaxt="n"
# Finally, if you are going to use par(new=TRUE), you are creating an 
# independent y-axis, and you should leave room in the right margin to draw it
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") # I would also change the axis label

# When drawing axes, avoid magic numbers
axis(side=1,at=weekly.times$X)
axis(side=2,at=seq(p1.ylim[1],p1.ylim[2],by=50))

# When drawing the errors, you might want to keep the color of the line
# but maybe make it a bit lighter so it isn't easily confused
# making the error bars fainter makes the figure much easier on the eyes
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)
# again in this plot, just use type="o" to accomplish in 1 line what you're doing in 2
plot(weekly.times$X,weekly.times$exercise.time, xlab='', ylab='', axes=FALSE, col="blue", type="o")
axis(side=4) # add the axis for the blue line
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)