year = 1990:1996 # Create a series of years
x1 = c(10, 15, 14, 7, 14, 10, 18) # Create some observation values for x1
x1ma = filter(x1, # Create a 4 period moving average for x1
filter = c(.25, .25, .25, .25), # Equal weights for the 4 periods
sides = 1) # Backwards looking moving avg.
x1ma # Display list output
as.double(x1ma) # Display as forced to numeric
# Plot of moving averate data --------------------------------------------------
png(filename = "illustrations/fig-9-2-moving avg-BW.png",
units = "in", # Set measurements in inches
res = 1200, # Set resolution at 1200dpi
width = 6, # Width at 6 inches
height = 4) # Height at 4 inches
par(mai = c(1, 1, .25, .25)) # Set margins for no title
plot(year, x1, # Plot x1 against year
ylim = c(0, 20), # Set y axis scale
type = "o", # Set as line plot with points
pch = 16) # Use solid point style
points(year, x1ma, # Add x1 moving avg (x1ma) to plot
type = "o", # Set type as line with points
col = "darkgray", # Set color to dark gray
pch = 16) # Use solid rather than open points
legend(x = 1996, y = 5, # Location of legend
xjust = 1, # Right justify legend box
legend =
c("x1", "4 Period Moving Avg"), # Legend Text
col = c("black", "darkgray"), # Legend Element Colors
pch = 16) # Legend Element Styles
dev.off() # Output png file
Figure 9-2: A Moving Avg Plot
Portfolio Categories: All Graphics and SGR Book Graphics.