I'm hoping to create a plot that shows a running average over a scatterplot of the observed data. The data consists of observations of hares' coat color (Color) over time (Julian).
Color Julian
50 85
50 87
50 89
50 90
100 91
50 91
50 92
50 92
100 92
50 93
100 93
50 93
50 95
100 95
50 95
50 96
50 96
50 99
50 100
0 101
0 101
0 103
50 103
50 104
50 104
50 104
50 104
100 104
100 104
50 109
50 109
100 109
0 110
0 110
50 110
50 110
50 110
50 110
0 112
A friend wrote a function for me that calculates a running average of the color observations, but I can't figure out how to add the line (haresAveNoNa) into the plot.
The function:
haresAverage <- matrix( NA, max(hares$Julian), 3 )
for( i in 4:max(hares$Julian) ){
haresAverage[i,1]<-i
haresAverage[i,2]<-mean( hares$Color[ hares$Julian >= (i-3) &
hares$Julian <= (i+3)]
, na.rm=T )
haresAverage[i,3]<-sd( hares$Color[ hares$Julian >= (i-3) &
hares$Julian <= (i+3)]
, na.rm=T )
}
haresAveNoNa <- na.omit( haresAverage)
The plot:
p <- ggplot(hares, aes(Julian, Color))
p +
geom_jitter(width = 1, height = 5, color="blue", alpha = .65)
Can you please help me add the running average 'haresAveNoNa' into the plot? Thanks very much!