0

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"))
  • Welcome to SO! Unfortunately we neither see anything nor could we run your code as we don't have your data. To help you, we need [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data best shared via `dput()`. – stefan Mar 09 '23 at 10:30
  • I really appreciate your answer, thank you! You're right, next time i will do that.I was actually able to solve it, with limits=c(as.Date('1999-01-16'), as.Date('2021-01-16')), expand=c(0, .9) – Artesanal_Panettone Mar 09 '23 at 11:00
  • Please provide enough code so others can better understand or reproduce the problem. – Community Mar 09 '23 at 12:02

1 Answers1

0

This was my original 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"))

The way I solved it was by adding an attribute to scale_x_date ():

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"),
                 limits=c(as.Date('1999-01-16'), as.Date('2021-01-16')),
                 expand=c(0, .9))

This gets rid of the empty space and doesn't show me the year 1998 or 2022.