1

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

Geiser
  • 1,054
  • 1
  • 12
  • 28
  • Care to make your code [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? I doubt many SO users have the R object `query` in their R environments. – shekeine Sep 11 '15 at 09:39
  • 1
    Set the second limit to `NA`, not `NULL`. – aosmith Sep 11 '15 at 15:32

2 Answers2

1

limits needs a vector of length two to work, so omitting the upper limit won't work. Also, as I see it, rangeDate is not an element of your data.frame query which ggplot refers to. Maybe something like

... + scale_x_datetime(data=input, limits=c(min(rangeDate), max(rangeDate))

will work.

EDIT:

Alternatively, put your variable rangeDate in the data.frame you use in ggplot().

ttlngr
  • 43
  • 7
  • 1
    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`. – aosmith Sep 11 '15 at 15:31
1

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!

Geiser
  • 1,054
  • 1
  • 12
  • 28