- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我非常感谢这个论坛中乐于助人的社区的帮助,解决我在使用 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.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()
之后,我没有看到任何错误栏以与主题类似帖子的原始答案相同的方式生成: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)
第一个图的输出,我想在其中添加误差线。在此情节之后,我还运行了箭头功能:
我在图表中包含两个图的输出。我最终希望这两个图表都有误差线。我也试过在这里运行 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)
但是,我现在有如下新数据,它没有得到适合的误差线。
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
使用相同的代码,我得到以下图:
你知道我在轴代码中哪里出错导致蓝线的错误栏不显示吗?
谢谢。
最佳答案
您遇到了两个主要问题。首先,您向 arrows 的
;我已将它们更改为 x0
和 x1
参数提供了一个 y 轴变量 weekly.times$sitting.time
()weekly.times$X
,即星期,位于您的 x 轴上。其次,您尝试在调用 par(new=TRUE) 并添加练习时间后添加箭头,练习时间的范围在 ~60 到 ~100 之间,因为 ylim
不是set 和你的 arrows
在 500-600 范围内,它们将被创建远离情节(即使第一个问题已经解决)。
以下代码对您的原始代码进行了最少的更改,以生成合适的图形。请注意,您没有提供对象 sitCI.down
和 sitCI.up
的数据,所以我只在坐着时间周围做了 ±10%。此外,我首先将 dput()
的输出包含在我从您的问题中手动复制的 data.frame 中;将来,如果您对数据使用 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")
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)
关于r - 箭头函数不适用于 R 中的错误栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31138929/
我无法在附加行中显示“真”、“假”、"is"和“否”按钮。 我在这里有一个应用程序:Application 请按照以下步骤使用应用程序: 1。当你打开应用程序时,你会看到一个绿色的加号按钮,点击 在此
我是一名优秀的程序员,十分优秀!