Search code examples
rdataframeggplot2plotdplyr

Scale_x_date is displaying years outside of the limits?


I am trying to plot daily mean, minimum and maximum temperatures data across years. My start and end dates are 2000-09-11 and 2006-07-22. So my Year variable on x-axis should range from 2000 to 2006. However, the figure is showing years from 20001 and 2006.

Here are my code and figure:

fig <- ggplot(df, aes(date, value, color = temp_type )) +
  geom_point() +
  scale_x_date(date_breaks = "1 year", date_labels  = "%Y", limits = as.Date(c("2000-09-11", "2006-07-22"))) +
  theme_few() + 
  labs(x = "Year", y = "Temperature (°C)")

fig

enter image description here

Reproducible example:


df <- structure(list(year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
                                        2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                        2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                        3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                        4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
                                        5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 
                                        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
                                        6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
                                        7L, 7L, 7L, 7L, 7L), levels = c("2000", "2001", "2002", "2003", 
                                                                        "2004", "2005", "2006", "2007", "2008", "2009", "2010"), class = "factor"), 
                     date = structure(c(11211, 11211, 11211, 11212, 11212, 11212, 
                                        11213, 11213, 11213, 11214, 11214, 11214, 11215, 11215, 11215, 
                                        11216, 11216, 11216, 11217, 11217, 11443, 11443, 11443, 11444, 
                                        11444, 11444, 11445, 11445, 11445, 11446, 11446, 11446, 11447, 
                                        11447, 11447, 11448, 11448, 11448, 11449, 11449, 11759, 11759, 
                                        11759, 11760, 11760, 11760, 11761, 11761, 11761, 11762, 11762, 
                                        11762, 11763, 11763, 11763, 11764, 11764, 11764, 11765, 11765, 
                                        12270, 12270, 12270, 12271, 12271, 12271, 12272, 12272, 12272, 
                                        12273, 12273, 12273, 12274, 12274, 12274, 12275, 12275, 12275, 
                                        12276, 12276, 12539, 12539, 12539, 12540, 12540, 12540, 12541, 
                                        12541, 12541, 12542, 12542, 12542, 12543, 12543, 12543, 12544, 
                                        12544, 12544, 12545, 12545, 12979, 12979, 12979, 12980, 12980, 
                                        12980, 12981, 12981, 12981, 12982, 12982, 12982, 12983, 12983, 
                                        12983, 12984, 12984, 12984, 12985, 12985, 13345, 13345, 13345, 
                                        13346, 13346, 13346, 13347, 13347, 13347, 13348, 13348, 13348, 
                                        13349, 13349, 13349, 13350, 13350, 13350, 13351, 13351), class = "Date"), 
                     value = c(12.4, 25, 18.052778, 14.3, 27.4, 20.234722, 15.1, 
                               27.9, 20.927778, 16.1, 26.4, 20.576389, 12.7, 23.8, 18.644444, 
                               16.1, 23.5, 18.738889, 14.4, 25.6, 5.7, 18, 12, 3.8, 19.1, 
                               11.766667, 6, 20.3, 13.479167, 6.4, 19.7, 13.002778, 8, 17.9, 
                               12.451389, 5.4, 19.2, 11.943056, 9.4, 13.3, 9.9, 12.7, 11.144444, 
                               8.7, 11.4, 9.9888889, 7, 16.1, 10.729167, 6.2, 17, 10.291667, 
                               6.3, 15.3, 11.469444, 8.1, 13, 10.586111, 3.8, 15.2, 15.5, 
                               26.9, 22.623611, 10.8, 22.6, 17.598611, 13.4, 24.5, 19.313889, 
                               11.1, 27.8, 19.120833, 15, 25, 19.134722, 14.4, 24.9, 18.151389, 
                               12.5, 23.4, 10.7, 14.1, 12.488889, 7, 15.4, 11.084722, 5.9, 
                               15.9, 10.633333, 8, 14, 10.548611, 9.2, 20, 14.088889, 12.3, 
                               22.4, 16.488889, 11.7, 19.6, 13.1, 24.9, 18.970833, 12.9, 
                               26.5, 19.805556, 13.2, 30.1, 22.472222, 17, 33.7, 25.606944, 
                               21.3, 37.9, 29.334722, 22.1, 33.8, 26.6375, 19.5, 29.6, 13.4, 
                               20.8, 17.056944, 12.3, 30.2, 20.911111, 10.4, 23.2, 18.676389, 
                               8.3, 16.3, 13.304167, 7.5, 24.8, 16.747222, 15.5, 25.2, 20.813889, 
                               12.6, 26.2), temp_type = c("min_temp", "max_temp", "mean_temp", 
                                                          "min_temp", "max_temp", "mean_temp", "min_temp", "max_temp", 
                                                          "mean_temp", "min_temp", "max_temp", "mean_temp", "min_temp", 
                                                          "max_temp", "mean_temp", "min_temp", "max_temp", "mean_temp", 
                                                          "min_temp", "max_temp", "min_temp", "max_temp", "mean_temp", 
                                                          "min_temp", "max_temp", "mean_temp", "min_temp", "max_temp", 
                                                          "mean_temp", "min_temp", "max_temp", "mean_temp", "min_temp", 
                                                          "max_temp", "mean_temp", "min_temp", "max_temp", "mean_temp", 
                                                          "min_temp", "max_temp", "min_temp", "max_temp", "mean_temp", 
                                                          "min_temp", "max_temp", "mean_temp", "min_temp", "max_temp", 
                                                          "mean_temp", "min_temp", "max_temp", "mean_temp", "min_temp", 
                                                          "max_temp", "mean_temp", "min_temp", "max_temp", "mean_temp", 
                                                          "min_temp", "max_temp", "min_temp", "max_temp", "mean_temp", 
                                                          "min_temp", "max_temp", "mean_temp", "min_temp", "max_temp", 
                                                          "mean_temp", "min_temp", "max_temp", "mean_temp", "min_temp", 
                                                          "max_temp", "mean_temp", "min_temp", "max_temp", "mean_temp", 
                                                          "min_temp", "max_temp", "min_temp", "max_temp", "mean_temp", 
                                                          "min_temp", "max_temp", "mean_temp", "min_temp", "max_temp", 
                                                          "mean_temp", "min_temp", "max_temp", "mean_temp", "min_temp", 
                                                          "max_temp", "mean_temp", "min_temp", "max_temp", "mean_temp", 
                                                          "min_temp", "max_temp", "min_temp", "max_temp", "mean_temp", 
                                                          "min_temp", "max_temp", "mean_temp", "min_temp", "max_temp", 
                                                          "mean_temp", "min_temp", "max_temp", "mean_temp", "min_temp", 
                                                          "max_temp", "mean_temp", "min_temp", "max_temp", "mean_temp", 
                                                          "min_temp", "max_temp", "min_temp", "max_temp", "mean_temp", 
                                                          "min_temp", "max_temp", "mean_temp", "min_temp", "max_temp", 
                                                          "mean_temp", "min_temp", "max_temp", "mean_temp", "min_temp", 
                                                          "max_temp", "mean_temp", "min_temp", "max_temp", "mean_temp", 
                                                          "min_temp", "max_temp")), class = c("grouped_df", "tbl_df", 
                                                                                              "tbl", "data.frame"), row.names = c(NA, -140L), groups = structure(list(
                                                                                                year = structure(1:7, levels = c("2000", "2001", "2002", 
                                                                                                                                 "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010"
                                                                                                ), class = "factor"), .rows = structure(list(1:20, 21:40, 
                                                                                                                                             41:60, 61:80, 81:100, 101:120, 121:140), ptype = integer(0), class = c("vctrs_list_of", 
                                                                                                                                                                                                                    "vctrs_vctr", "list"))), row.names = c(NA, -7L), .drop = TRUE, class = c("tbl_df", 
                                                                                                                                                                                                                                                                                             "tbl", "data. Frame")))

Solution

  • The dates are shown exactly as they are supposed to. September 2000 is closer to the start of 2001 than it is to 2000, so the points here show up a little to the left of 2001. If you just want the year of the points, use something like lubridate::year, and use a simple numerical axis (i.e. just the default):

    ggplot(df, aes(lubridate::year(date), value, color = temp_type )) +
      geom_point() +
      scale_x_continuous(breaks = 2000:2006) +
      theme_classic() + 
      theme(panel.border = element_rect(fill = NA)) +
      labs(x = "Year", y = "Temperature (°C)")
    

    enter image description here