5

I'm currently analyzing a dataset of GSR values. I first had to transform my unix values into readable data and then create a plot of the GSR values in function of time.

That is how the dataset looks like: enter image description here

This is my code and graphenter image description here:

veranda <- ggplot(gsr_veranda, aes(as.POSIXct(Date, origin = "1970-01-01"), Values)) +
               geom_line() +
               scale_x_datetime(date_labels = "%H:%M:%s") +

I wanted to zoom into the graph and look at a specific time I tried this code:

veranda <- ggplot(gsr_veranda, aes(as.POSIXct(Date, origin = "1970-01-01"), Values)) +
               geom_line() +
               scale_x_datetime(date_labels = "%H:%M:%s") +
               scale_x_continuous(limits = c("11:05:02", "11:05:03"))

However, I still get this error: Error in as.POSIXct.numeric(value) : 'origin' must be supplied

But the origin was already supplied when I transformed my unix into readable data and again in my ggplot code.

How can I fix this?

Sarah.d
  • 123
  • 1
  • 2
  • 12
  • I also tried coord_cartesian(xlim = ()). But then I get this error: Error: Invalid input: time_trans works with objects of class POSIXct only – Sarah.d Jul 24 '18 at 13:19
  • 3
    You should only have one `scale_x_XXX`, not both `scale_x_datetime()` and `scale_x_continuous`. For more specific advice, it would be useful if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), e.g. by using `dput()`. Currently we don't have sight of what your dataset `gsr_veranda` actually looks like. – Z.Lin Jul 24 '18 at 13:23
  • I added a capture of my dataset! – Sarah.d Jul 24 '18 at 13:27

1 Answers1

3

To fix this, you should pass your limits in the scale_x_datetime() function. Also, they must be on a POSIXct format. The following code does that:

lim <- as.POSIXct(c("2018-05-07 11:05:02", "2018-05-07 11:05:03"),  origin = "1970-01-01")

veranda <- ggplot(gsr_veranda, aes(as.POSIXct(Date, origin = "1970-01-01"), Values)) +
    geom_line() +
    scale_x_datetime(date_labels = "%H:%M:%s",limits=lim) 
André Costa
  • 377
  • 1
  • 11