Search code examples
rggplot2graphfigure

How to set the Y and X values in the graph origin


I am trying to plot the following graph:

enter image description here

But the Y axis ticks are not close to the colors lines.

How can I make the rectangule complete the full background area?

This is my code:

plot1 <- ggplot(data = data, aes(x = X, y = Y)) +
  geom_point()+
  scale_y_continuous(breaks = c(0,40,80,120,200,400),limits = c(0,400),expand = c(0, 0))+ # Define the corresponding y-axis tick labels
  labs(x = "", y = "Y") +  # Adicione rótulos aos eixos, se necessário+
  theme_minimal()+
  theme(panel.grid.major.y = element_blank(),  # Remove major grid lines for y-axis
        panel.grid.minor.y = element_blank())+# Remove minor grid lines for y-axis
  coord_cartesian(xlim = as.Date(c("2021-12-21", "2023-01-01"),ylim = c(0, 400)))  # Definir a origem do gráfico para junho de 2022
plot2 <- plot1 + annotate("rect", 
                          ymin = c(0,40,80,120,200),
                          ymax = c(40,80,120,200,400),
                          xmin = as.Date("2021-12-21"), xmax = as.Date("2022-12-30"), fill = c('green', "yellow","orange","red","purple"), alpha=0.5)

I've already tried to change the limits on xmin and xmax, but as the X axis is a DATE TYPE, it does not work. Also, I've tried to specify the Xaxis as -Inf and Inf, but also it did not worked since is a DATE TYPE.

I am expect to let my entire baground with color.

Sample Data


Solution

  • Does this do what you want? If so, use scale_x_date(expand = c(0,0) instead of coord_cartesian(...).

    library(tidyverse)
    
    # Had to create an example dataset
    set.seed(222)
    
    data <- data.frame(
      X = seq(as.Date('2022-01-01'),
                 as.Date('2022-12-31'),
                 '1 day'),
      Y = rnorm(365, 200, 50)
    )
    
    # Here is a similar plot to the one posted 
    plot1 <- ggplot(data = data, aes(x = X, y = Y)) +
      geom_point()+
      scale_y_continuous(breaks = c(0,40,80,120,200,400),limits = c(0,400),expand = c(0, 0))+ # Define the corresponding y-axis tick labels
      labs(x = "", y = "Y") +  # Adicione rótulos aos eixos, se necessário+
      theme_minimal()+
      theme(panel.grid.major.y = element_blank(),  # Remove major grid lines for y-axis
            panel.grid.minor.y = element_blank())+# Remove minor grid lines for y-axis
      coord_cartesian(xlim = as.Date(c("2021-12-21", "2023-01-01"),ylim = c(0, 400)))  # Definir a origem do gráfico para junho de 2022
    
    plot2 <- plot1 + annotate("rect", 
                              ymin = c(0,40,80,120,200),
                              ymax = c(40,80,120,200,400),
                              xmin = as.Date("2021-12-21"), xmax = as.Date("2022-12-30"), fill = c('green', "yellow","orange","red","purple"), alpha=0.5)
    
    plot2
    

    # Here is the updated plot using scale_x_date(expand = c(0,0) instead of coord_cartesian(...)
    data %>%
      ggplot(aes(x = X, y = Y)) +
      geom_point() +
      scale_y_continuous(breaks = c(0,40,80,120,200,400),
                         limits = c(0,400),
                         expand = c(0, 0)) +
      scale_x_date(date_breaks = "2 month",
                   date_labels = "%b %Y",
                   expand = c(0,0)) +
      labs(x = "", y = "Y") +
      theme_minimal() +
      theme(panel.grid.major.y = element_blank(),
            panel.grid.minor.y = element_blank(),
            panel.grid.minor.x = element_blank()) +
      annotate("rect", 
               ymin = c(0,40,80,120,200),
               ymax = c(40,80,120,200,400),
               xmin = as.Date("2021-12-21"), xmax = as.Date("2022-12-30"), fill = c('green', "yellow","orange","red","purple"), alpha=0.5)
    

    Created on 2024-05-14 with reprex v2.1.0