I've plotted data from 1999-2021 and I want the chart area to be trimmed so that I don't see the empty space of 1998 or 2023. I want the line to begin in 1999 and end on 2021, or exactly the amount of data I have.I've tried a few things like: scale_x_discrete(), coord_cartesian(), I've also tried using limits=c(as.Date('1999-01-16'), as.Date('2021-12-16'). I just can't get it to cutout the area of the graphic I don't want to see.
Can anyone help me out?
Here is my code:
xcfoodtotal$date <- as.Date(xcfoodtotal$date)
p <- ggplot(data = xcfoodtotal, aes(x=date, y=consumption)) +
geom_line(color="turquoise4", size=1) +
geom_smooth(method=lm, se=TRUE, linetype = "dashed", col="blue", size=0.5) +
theme_bw() +
labs(x="", y="kg/Capita") +
theme(plot.title = element_text(hjust=0.5, size=15, face="bold"),
axis.text.x=element_text(angle=60, hjust=1, face = "bold"),
axis.text.y=element_text(hjust=1, face = "bold"),
text = element_text(size = 16, family="Helvetica"))
p + scale_x_date(date_breaks = "1 year", labels = date_format("%Y"))