Activity: Plot the Monthly Closing Stock Prices and the Mean Values

Steps for Completion:

  1. Use the strftime command to get the month from each date and make another variable (Month), as follows:
df_fb$Month <- strftime(df_fb$Date,"%m")
  1. Change the month to a numerical value by using as.numeric:
df_fb$Month <- as.numeric(df_fb$Month)
  1. Now, use ggplot to make a plot of closing prices versus months.
  2. Plot the data using geom_point (color=red).
  3. Change the x scale to show each month, and label the x-axis, such that each month is shown.
  4. Title your plot Monthly closing stock prices: Facebook.
  5. Use geom_line(stat='summary',fun.y=mean) to plot the mean.

Outcome:

The complete code is shown as follows:

ggplot(df_fb, aes(Month,Close)) + geom_point(color="red",alpha=1/2,position ...

Get Applied Data Visualization with R and ggplot2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.