Search code examples
rdateggplot2scale

Set only lower limits in ggplot2 scale_x_datetime(limits)


I have a DB that reads a value every 5 minutes. In order to display the data, I have a Shiny App that plots it each time you connect to it. I recently added a daterangeinput, in order to select the data to plot.

The problem is when I plot the data of the same day. The display is small (because it's representing until the same night althought data doesn't exist yet) while using scale_x_datetime(limits). Here's an example:

enter image description here

I don't mind the lower limits, as they will always exist. I would like to set the lower limits and let the upper limit be free (and the last value to appear in the right border of the plot).

I tried setting the second limit to NULL or not even putting it but it throws me errors.

Here's the code:

ggplot(query, aes(x=DateTime, y=Freq, group=1)) + 
  geom_line() +
  labs(x="Time", y="Freq") +
  ggtitle("PLOT") +
  scale_y_continuous(breaks= pretty_breaks()) +
  scale_x_datetime(limits = as.POSIXct(input$rangeDate))

The data frame query looks like this, for example:

             DateTime       Freq
1 2015-09-11 09:22:57         10
2 2015-09-11 09:27:57         10
3 2015-09-11 09:32:57         11

Maybe the solution can be doing this in other way. I will higly appreciate your comments and answers.

Thanks


Solution

  • As aosmith said,

    You can't omit the upper limit, but you can use NA for one of the limits that you want automatically calculated. See the help page for ggplot2::ylim

    Thank you!