0

I wanted to remove the space between plotted line and y-axis as shown in Image 1. But when I have tried it, the x-axis text has been removed as shown in Image 2. I got some help from this question, but my case is different because I have Dateformat column in my x-axis.

Image 1 Image 2

This is the code I used:

# Plot SSI3 for URB
URB_SSI3 <- URB %>%  ggplot2::ggplot(aes(x = Date, y = SSI3, color = datasets)) + 
  geom_line(linewidth=0.3, linetype="solid") +
  scale_color_manual(name=NULL, breaks = c("Observed", "ERA5.Land",
                                           "GLDAS", "GLEAM"),
                     values = c("Orange", "Red", "purple", "black"),
                     labels = c("Observed", "ERA5-Land",
                                "GLDAS", "GLEAM"))+
  #title and legend
  scale_y_continuous(limits = c(-3, 3), breaks = -3:3) +
  scale_x_continuous(breaks= seq(min(year(URB$Date)), max(year(URB$Date)), by = 2),
                     labels = seq(min(year(URB$Date)), max(year(URB$Date)), by = 2),
                     expand = c(0, 0)) +
  ggtitle("URB-SSI3") +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.text.x = element_text(colour = "black"),
        axis.text.y = element_text(colour = "black"),
        legend.background = element_blank(),
        legend.position = c(0.5, 0.1),
        legend.direction = "horizontal",
        legend.key = element_rect(fill = "transparent"),
        legend.key.width = unit(30, "pt")
        # legend.justification = c(0.3, 0.5)
        ) +
  guides(fill="none")
URB_SSI3

This is my data in google drive as .rds file.

Any thoughts please?

r2evans
  • 141,215
  • 6
  • 77
  • 149
Mukhtar Abdi
  • 391
  • 1
  • 12
  • What happens if you leave out the `breaks` and `labels` details and just include `expand = c(0,0)` in `scale_x_continuous` ... ? – Ben Bolker Jun 21 '23 at 19:48
  • btw, neither `Date` nor `SSI3` columns are not present in your sample data – r2evans Jun 21 '23 at 19:51
  • 1
    As you deal with dates I would try `scale_x_date(date_breaks = "2 years", date_labels = "%Y", expand = c(0, 0))` to have a break for each second year, a label whichs shows only the year and to remove the expansion. – stefan Jun 21 '23 at 20:10
  • Stevan's answer has worked for me. thanks – Mukhtar Abdi Jun 22 '23 at 18:28

0 Answers0