【问题标题】:Error bars in R with Two atomic vectorsR中带有两个原子向量的误差线
【发布时间】:2015-04-18 11:50:01
【问题描述】:

我有以下原子向量:

Atom_1

Size Square Standard_Deviation
10     0.20        0.56
20     0.40        0.36
30     0.34        0.50
40     0.26        0.33

Atom_2

Size Square Standard_Deviation
10     0.20        0.56
20     0.40        0.36
30     0.34        0.50
40     0.26        0.33

我绘制图表使用,

plot(Atom_1, col="red")
points(Atom_2, col="blue")

但是我怎样才能在原子向量的相应标准偏差列中添加误差线?

我尝试了解决方案:Add error bars to show standard deviation on a plot in R

d = data.frame(
  x <- as.numeric(Atom_1[, 1])
  , y <- as.numeric(Atom_1[, 2])
  , sd <- as.numeric(Atom_1[, 3])
)

plot(d$x, d$y, type="n", ylim = c(0, 10))
with (
  data = d
  , expr = errbar(x, y, y+sd, y-sd)
)

但它什么也没显示。我怎样才能添加 Atom_2 情节呢?

注意:我之前的问题不清楚,所以我删除了这个问题。

【问题讨论】:

  • d 的列名乱了。要修复它,请将d = data.frame(...) 中的= 替换为&lt;-
  • @MaratTalipov 完成!但无法让它工作。
  • 顺便说一下,您的Atom_1Atom_2 不是原子向量——它们是数据帧

标签: r


【解决方案1】:

我不确定您的代码到底哪里出了问题,但下面是有效的代码:

library(Hmisc)

d <- setNames(Atom_1,c('x','y','sd'))
#    x    y   sd
# 1 10 0.20 0.56
# 2 20 0.40 0.36
# 3 30 0.34 0.50
# 4 40 0.26 0.33

with(d, errbar(x, y, y+sd, y-sd))

[更新]

如果您想从两个数据集中放置误差线,您可以使用另一个调用 errbaradd=TRUE

with(Atom_1, errbar(Size, Square, Square+Standard_Deviation, Square-Standard_Deviation,col='red',errbar.col='red'))
with(Atom_2, errbar(Size, Square, Square+Standard_Deviation, Square-Standard_Deviation,col='blue',errbar.col='blue',add=T))

Atom_1Atom_2 是您的样本数据集(Atom_2 进行了修改以获得更好的视图):

Atom_1 <- structure(list(Size = c(10L, 20L, 30L, 40L), Square = c(0.2, 
0.4, 0.34, 0.26), Standard_Deviation = c(0.56, 0.36, 0.5, 0.33
)), .Names = c("Size", "Square", "Standard_Deviation"), class = "data.frame", row.names = c(NA, 
-4L))

Atom_2 <- structure(list(Size = c(12L, 22L, 28L, 38L), Square = c(0.2, 
0.4, 0.34, 0.26), Standard_Deviation = c(0.56, 0.36, 0.5, 0.33
)), .Names = c("Size", "Square", "Standard_Deviation"), class = "data.frame", row.names = c(NA, 
-4L))

【讨论】:

  • 如何添加 atom_2 ?
  • @MaratTalivpov 我想将 atom_2 添加到具有相同 x 值的同一图中。所以 Atom_1 和 atom_2 将与它们对应的误差线在同一个图中(我可以通过颜色区分它们)
猜你喜欢
  • 2014-07-31
  • 1970-01-01
  • 2015-10-12
  • 1970-01-01
  • 1970-01-01
  • 2018-02-22
  • 2015-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多