business-science/tidyquant

natural order of numbers for x not observed

smirarab opened this issue · 0 comments

If the x aesthetic has a natural order (e.g., is simply a number), the data should be sorted by that order before taking the moving average in my opinion. This does not happen. See examples below.

x=(1:10000)*10000
td=data.frame(x=x,y=ifelse(40000000<x & 50000000>x,1,0))

ggplot(aes(x=x,y=y),                
       data=td)+
  geom_point(alpha=0.25,size=0.2)+
  geom_ma(color="red",n = 100,linetype=1,alpha=0.5)

ggplot(aes(x=x,y=y),                
       data=td[order(as.character(x)),])+
  geom_point(alpha=0.25,size=0.2)+
  geom_ma(color="red",n = 100,linetype=1,alpha=0.5)

The only difference between the first figure and the second is the order of lines (in the second one rows are lexicographically ordered instead of numerically). We get a completely wrong picture with the second one. It's computing the moving average for unordered data but it of course will be drawn with correct (numerical) ordering on the x axis. So moving average does not correspond to the x-axis correctly. geom_ma should reorder values based on x before computing averages.