箭头功能不适用于R

时间:2015-06-30 12:56:42

标签: r plot

我非常感谢这个论坛中乐于助人的社区的帮助,我有一个与R有关的小问题,以及在尝试绘制错误栏时的箭头功能。我选择使用箭头功能来生成我的错误栏,以便在添加到我已经创建的图表时简单。

我使用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

在表格中,sitting.timeexercise.time是我的y值,X是x值,se.sit.timese.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()之后,我没有看到生成的任何错误栏与原始答案相同的方式:Scatter plot with error bars < / p>

arrows(weekly.times$sitting.time, sitCI.down, 
  weekly.times$sitting.time, sitCI.up, length=0.05, angle=90, code=3)

第一个图的输出,我想添加误差线。我也在这个情节之后运行箭头功能:

enter image description here

我的输出图中的两个图。我最终希望两个图都有误差线。我也试图在这里运行箭头()无效:

enter image description here

有谁知道我可能做错了什么,为什么错误图没有出现在图片中?

在关注@rbatt的解决方案后,我得到了图表,可以使用如下错误图:

enter image description here

我使用了以下代码:

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

使用相同的代码我得到以下图:

enter image description here

你知道轴代码在哪里出错导致蓝线的误差条不显示吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

你有两个主要问题。首先,您为weekly.times$sitting.time的{​​{1}}和x0参数提供了y轴变量x1;我已将这些更改为arrows(),即x轴上的周。其次,您试图在调用par(new = TRUE)后添加箭头,并添加运动时间,范围介于~60和~100之间,因为weekly.times$X未设置且您的ylim在500-600范围内,它们将在情节之外创建(即使第一个问题已经解决)。

下面是对原始代码进行微小更改以生成合适数字的代码。请注意,您没有提供对象arrowssitCI.down的数据,所以我只是在休息时间内做了±10%。另外,我首先在我从您的问题手动复制的data.frame中包含sitCI.up的输出;在将来,如果您对数据使用dput()并将输出复制到您的问题中,那么将会更轻松地重现您的结果。

使用dput

的输出创建数据集
dput()

生成所需的图,只需对原始代码进行最少的更改

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")

enter image description here

所以上面的代码应该可以解决你的问题。但是,我想就你的身材提供一些额外的帮助/风格建议。请参阅下面的代码。

更清洁,更精简的方法(我的意见)

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)

enter image description here