Home > Software engineering >  Remove excess space in ggplot when x axis has dates
Remove excess space in ggplot when x axis has dates

Time:05-19

My problem is that with ggplot, when the x-axis contains dates a little too muuch excess space is given in the plot on the left side
Is there any possibility for the red space to be removed? so that the whole curve and x-axis is shifted to the left.

library(tidyverse)
set.seed(138)
mydat <- (as.Date("2021-04-01") 0:99) %>% as.data.frame()
y <- rnorm(100) 
mydat$val <- y
names(mydat) <- c("Time", "Value")

ggplot(mydat, aes(x=as.Date(Time))) geom_line(aes(y=Value)) 
  scale_x_date(breaks = c(seq(as.Date('2021-04-01'), as.Date('2021-08-01'), by="2 week")) , date_labels = "%d-%b-%y ", limits=c(as.Date('2021-04-01'), as.Date('2021-07-09'))) theme(legend.position="bottom") 
  theme(
    axis.text.x = element_text(angle=45,size=10, vjust = 0.5))

enter image description here

CodePudding user response:

Add the expand =c(0,0) parameter in the scale_x_date

enter image description here

  • Related