0

I am trying to plot the SP500 Index near the 2004 Presidential Elections.

The data are:

Date        SP500
2004-05-01  1103
2004-06-01  1133
2004-07-01  1106
2004-08-01  1089
2004-09-01  1118
2004-10-01  1117
2004-11-01  1169
2004-12-01  1199
2005-01-01  1181

My code is the following:

library(ggplot2)
library(ggthemes)
library(lubridate)

ggplot(data_2004 , aes(x = Date, y = SP500)) +
  geom_path(color = "red", size = 2) + 
  theme_economist() +
  ggtitle("S&P 500 Index Tumbles because of US Presidential Elections") +
  ylab("SP500 Index") + 
  xlab("Date") + 
  scale_x_continuous(
    breaks = data_2004$Date,
    labels = c('5/2004', '6/2004', '7/2004','8/2004', '9/2004', '10/2004', 
               '11/2004', '12/2004', '1/2005'))

I get the following exception:

ERROR while rich displaying an object: Error in as.Date.numeric(value): 'origin' must be supplied

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
user8270077
  • 4,621
  • 17
  • 75
  • 140
  • 1
    Make sure your date is of class `Date` and use `scale_x_date(date_breaks = "1 month, date_labels = "%m/%Y")` – Henrik Jun 18 '18 at 12:30
  • Possible duplicate of [Formatting dates with scale_x_date in ggplot2](https://stackoverflow.com/questions/10576095/formatting-dates-with-scale-x-date-in-ggplot2); [Formatting dates on X axis in ggplot2](https://stackoverflow.com/questions/11748384/formatting-dates-on-x-axis-in-ggplot2) – Henrik Jun 18 '18 at 12:31

0 Answers0