-1

I have the following atomic vectors:

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

I plot the graph using,

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

But how can I add error bars with corresponding Standard deviation column of the atomic vectors?

I tried solution in: 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)
)

But it displayed nothing. How can I add Atom_2 plot with it as well?

Note: My previous question was not clear, so I deleted to make this clear one.

Community
  • 1
  • 1
Gworld
  • 103
  • 10

1 Answers1

1

I am not sure where exactly is the problem with your code, but here is the code that works:

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

enter image description here

[Update]

If you'd like to put error bars from two data sets, you can use another call to errbar with add=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))

enter image description here

where Atom_1 and Atom_2 are your sample data sets (Atom_2 was modified for better view):

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))
Marat Talipov
  • 13,064
  • 5
  • 34
  • 53
  • How can I add atom_2 ? – Gworld Feb 17 '15 at 18:12
  • @MaratTalivpov I want to add atom_2 to the same plot with same x values. So Atom_1 and atom_2 will be in the same plot with their corresponding error bars (i can distinguish them by colors) – Gworld Feb 17 '15 at 18:18