-1

Suppose I have the following data, how can plot 2 sigma lines around its mean and how to color them red when a point is above the 2 sigma lines.

20  1   15  19  23
19  2   14  20  66
18  3   13  3   34
17  4   12  4   12
16  5   11  5   45
15  6   10  6   19
14  7   20  7   27
13  8   19  8   15
12  9   18  8   19
11  10  17  9   43
10  11  16  10  23
9   12  15  11  31
8   13  1   12  41
7   14  14  13  24
6   15  13  14  29
5   16  12  15  16
4   17  11  16  52
3   18  10  17  12
2   19  9   20  15
1   20  8   19  31
zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 2
    what have you tried so far? you can put together a solution with `?mean` and `?sd` (to find the 2-sigma limits) and `plot(...,col=ifelse(...,"black","red"))` ... – Ben Bolker Oct 19 '16 at 21:19
  • 2
    Please show what you have tried so far. – USER_1 Oct 19 '16 at 21:28
  • I am a newbie and in learning stage. SO really helps me in many ways. – Analytics_Lover Oct 19 '16 at 21:33
  • yes, being a beginner is fine, but hopefully you have tried *something* before asking here, and more people will be more willing to pitch in with answers if you give even a relatively incoherent account of what you've tried/thought of ... does putting @ManojKumar's answer together with my hints above help you? – Ben Bolker Oct 19 '16 at 21:35
  • @BenBolker almost it does. I guess I can use SEGMENT for the rest plotting. just looking for the rest solution. Thanks everyone for helping me. – Analytics_Lover Oct 19 '16 at 21:38

1 Answers1

1

take the mean of the data,

say X <- c("your data")

meanX <- mean(X)

Then, calculate Standard deviation

sdX <- sd(X)

Then 2-sigma levels

Sigma2 <- meanX + c(-2, +2) * sdX

Finally, Plot them..

plot(X, typle = "l", main="", xlab="", ylab="", ) # just add the levels as you want
abline(h=meanX, col="red", lwd=2)
abline(h=Sigma2, lwd=2, col="blue")

Done!

Manoj Kumar
  • 5,273
  • 1
  • 26
  • 33