Skip to content

How to plot dates

7 messages · Sarah Goslee, Jeff Newmiller, Gregory Coats +1 more

#
My computer is an Apple MacBook. I do not have POSIX. 
The command
myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format = "%Y-%M-%d %H:%M:%OS")
yields the error
Error in `$<-.data.frame`(`*tmp*`, datetime, value = numeric(0)) : 
  replacement has 0 rows, data has 13
Please advise, How to proceed?
Greg Coats
X2021.03.11.10.00.00
1  2021-03-11 14:17:00
2  2021-03-12 05:16:46
3  2021-03-12 09:17:02
4  2021-03-12 13:31:43
5  2021-03-12 22:00:32
6  2021-03-13 09:21:43
Error in `$<-.data.frame`(`*tmp*`, datetime, value = numeric(0)) : 
  replacement has 0 rows, data has 13
#
Hi,

It doesn't have anything to do with having a Mac - you have POSIX.

It's because something is wrong with your data import. Looking at the
head() output you provided, it looks like your data file does NOT have
a header, because there's no datetime column, and the column name is
actually X2021.03.11.10.00.0

So you specified a nonexistent column, and got a zero-length answer.

With correct specification, the as.POSIXct function works as expected on Mac:


myDat <- read.table(text =
"datetime
2021-03-11 10:00:00
2021-03-11 14:17:00
2021-03-12 05:16:46
2021-03-12 09:17:02
2021-03-12 13:31:43
2021-03-12 22:00:32
2021-03-13 09:21:43",
sep = ",", header = TRUE)


myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format =
"%Y-%M-%d %H:%M:%OS")

Sarah

On Tue, Mar 16, 2021 at 9:26 AM Gregory Coats via R-help
<r-help at r-project.org> wrote:

  
    
#
Sarah, Thank you. Yes, now as.POSIXct works.
But the ggplot command I was told to use yields an Error message, and there is no output plot.
Please help me. Greg
+ "datetime
+ 2021-03-11 10:00:00
+ 2021-03-11 14:17:00
+ 2021-03-12 05:16:46
+ 2021-03-12 09:17:02
+ 2021-03-12 13:31:43
+ 2021-03-12 22:00:32
+ 2021-03-13 09:21:43",
+ sep = ",", header = TRUE)
datetime
1 2021-03-11 10:00:00
2 2021-03-11 14:17:00
3 2021-03-12 05:16:46
4 2021-03-12 09:17:02
5 2021-03-12 13:31:43
6 2021-03-12 22:00:32
Error in FUN(X[[i]], ...) : object 'Y_Var' not found

  
  
#
You don't seem to have a Y_Var in your data. What is it that you want to plot?
On March 16, 2021 9:21:05 AM PDT, Gregory Coats via R-help <r-help at r-project.org> wrote:

  
    
#
I want to plot the date and time of the event, as reflected in data.
2021-03-11 10:00:00
Greg Coats
#
So what do you want quantity on the y-axis to be?
On March 16, 2021 11:45:32 AM PDT, Gregory Coats <gregcoats at me.com> wrote:

  
    
#
Hello,

I don't really understand what is to be plotted, just the time of the 
event? But what event?

Anyway, with the data read with Sarah's code, maybe


library(ggplot2)

ggplot(myDat, aes(x = datetime, y = 1)) +
   geom_linerange(aes(ymin = 0, ymax = 1), linetype = "dotted") +
   geom_point() +
   scale_x_datetime(breaks = myDat$datetime) +
   scale_y_continuous(labels = NULL) +
   ylab("event") +
   theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust=1),
         axis.ticks.y = element_blank())


Hope this helps,

Rui Barradas

?s 13:36 de 16/03/21, Sarah Goslee escreveu: