Home > OS >  Setting x-axis limits with scale_x_date in ggplot, missing boundaries
Setting x-axis limits with scale_x_date in ggplot, missing boundaries

Time:10-20

I am trying to plot a bargraph of a particular range of datapoints by setting the x-axis limits with scale_x_date but I find that this excludes the boundary of the limits vector.

Example below

df = data.frame(
  date = as.Date(c("2021-10-10","2021-10-11","2021-10-12","2021-10-13",
           "2021-10-14","2021-10-15","2021-10-16",
           "2021-10-17","2021-10-18","2021-10-19")),
  count = c(1,2,3,4,1,2,2,1,2,3)
)

a = ggplot(df, aes(x=date, y=count))  
  geom_bar(stat="identity", colour="black", fill="steelblue")  
  scale_x_date(limits = c(df$date[5], df$date[10]))

a

I am trying to display the data from date 5 to date 10 but this cuts off the data for date 5 and date 10.

How should I set the limits to include the boundaries?

CodePudding user response:

A quick fix:

a = ggplot(df, aes(x=date, y=count))  
     geom_bar(stat="identity", colour="black", fill="steelblue")  
     scale_x_date(limits = c(df$date[5]-1, df$date[10] 1))

CodePudding user response:

What happens with out-of-bounds values in a scale is determined by the oob argument. Because you have bars that are parameterised by xmin/xmax/ymin/ymax values internally, the first xmin and last xmax fall outside your limits (so these are considered out-of-bounds). You can keep out-of-bounds values with scales::oob_keep().

library(ggplot2)

df = data.frame(
  date = as.Date(c("2021-10-10","2021-10-11","2021-10-12","2021-10-13",
                   "2021-10-14","2021-10-15","2021-10-16",
                   "2021-10-17","2021-10-18","2021-10-19")),
  count = c(1,2,3,4,1,2,2,1,2,3)
)

p <- ggplot(df, aes(x=date, y=count))  
  geom_col(colour="black", fill="steelblue")

p  
  scale_x_date(limits = c(df$date[5], df$date[10]),
               oob = scales::oob_keep)

However, you most likely want to accommodate the first and last bars by expanding the limits to include their xmin/xmax values.

p  
  scale_x_date(limits = c(df$date[5] - 0.5, df$date[10]   0.5))
#> Warning: Removed 4 rows containing missing values (position_stack).

Created on 2021-10-20 by the reprex package (v2.0.1)

  • Related