Reputation: 3
I'm trying to plot the monthly sales data with RStudio, but the dates on the x-axis are not showing correctly.
My code :-
uc_ts_plot <- ggplot(monthly_sales, aes(DATE,DAUTONSA)) + geom_line(na.rm=TRUE) +
xlab("Month") + ylab("Auto Sales in Thousands") +
scale_x_date(labels = date_format(format= "%b-%Y"),
breaks = date_breaks("1 year")) +
stat_smooth(colour = "green")
uc_ts_plot
I expect the dates on the x-axis to be displayed as Jan-2011, Jan-2012, as shown here.
All I'm getting is a 0001-01 at the left end and a 0002-01 at the right end of the x-axis.
Upvotes: 0
Views: 717
Reputation: 389275
The plot which is shown is filtered between year 2011 and 2018 whereas the data you have is from 1967.
The below code produces the exact plot
library(tidyverse)
library(scales)
library(lubridate)
monthly_sales %>%
mutate(DATE = as.Date(DATE)) %>%
filter(year(DATE) >= 2011 & year(DATE) < 2018) %>%
ggplot() + aes(DATE,DAUTONSA) +
geom_line(na.rm=TRUE) +
xlab("Month") + ylab("Auto Sales in Thousands") +
scale_x_date(labels = date_format(format= "%b-%Y"),
breaks = date_breaks("1 year")) +
stat_smooth(colour = "green")
You can remove the filter
step to plot data for all the years but then it clutters the x-axis with lot of labels.
Upvotes: 1