-2

I think I have an error in my logic while reproducing a graph I found in this pdf here.

It should be fairly easy to do, but I have issues to plot a variable with its mean and standard deviation each in their own graph together, as can be seen in the example graph below. Did they do it with facet_grid() or facet_wrap()?

How can I plot an arbitrary variable in that way? In particular, I would not know how to plot the mean and sd over distance (or time).

Example graph: enter image description here

tonytonov
  • 25,060
  • 16
  • 82
  • 98
Til Hund
  • 1,543
  • 5
  • 21
  • 37
  • You need to calculate these 3 functions over Distance, melt the data over Distance and use `facet_grid` over Distance. There is nothing special about this graph – David Arenburg Jan 26 '15 at 11:39
  • Thank you, David, for your answer. Can you please help me out how to calculate the mean over distance on an arbitrary data set? – Til Hund Jan 26 '15 at 12:51

1 Answers1

1

Here's my approach to the solution outlined by @DavidArenburg (though I simplified the data a little, using simple cumulative statistics and a plain index):

library(tidyr)
library(dplyr)
library(TTR)
v <- rnorm(1000)
df <- data.frame(index = 1:1000, 
                 variable = v, 
                 mean = runMean(v, n=1, cumulative=TRUE), 
                 sd = runSD(v, n=1, cumulative=TRUE))
dd <- gather(df, facet, value, -index)
ggplot(dd, aes(x = index, y = value)) + 
  geom_path() +
  facet_grid(facet ~ .)

enter image description here

Bonus: illustration that sample mean and sd are unbiased (0 and 1, respectively).

tonytonov
  • 25,060
  • 16
  • 82
  • 98