How to plot dates

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
21 messages Options
12
Reply | Threaded
Open this post in threaded view
|

How to plot dates

R help mailing list-2
I store in a text file the dates and times that an event occurred.
How do I direct R to import these text characters, and store the year, month, day, hour, minute, and second as a date?
How do I then plot this series of dates?
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
2021-03-13 13:51:12
2021-03-13 18:03:13
2021-03-13 22:20:28
2021-03-14 08:59:03
2021-03-14 13:15:56
2021-03-14 17:25:23
2021-03-14 21:36:26
Greg Coats
[hidden email]


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

reichmaj
Gregory

myDat <- data.frame(datetime = c("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","2021-03-13 13:51:12",
           "2021-03-13 18:03:13","2021-03-13 22:20:28","2021-03-14
08:59:03","2021-03-14 13:15:56",
           "2021-03-14 17:25:23","2021-03-14 21:36:26"))

# convert date to date time object
myDat$ datetime <- as.POSIXlt(myDat$ datetime, tz = "", format = "%Y-%M-%d
%H:%M:%OS")

# plotting
ggplot(myDat, aes(datetime, <y variable>)) .....

Jeff

-----Original Message-----
From: R-help <[hidden email]> On Behalf Of Gregory Coats via
R-help
Sent: Monday, March 15, 2021 4:34 PM
To: r-help mailing list <[hidden email]>
Subject: [R] How to plot dates

I store in a text file the dates and times that an event occurred.
How do I direct R to import these text characters, and store the year,
month, day, hour, minute, and second as a date?
How do I then plot this series of dates?
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
2021-03-13 13:51:12
2021-03-13 18:03:13
2021-03-13 22:20:28
2021-03-14 08:59:03
2021-03-14 13:15:56
2021-03-14 17:25:23
2021-03-14 21:36:26
Greg Coats
[hidden email]


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

Jim Lemon-4
In reply to this post by R help mailing list-2
Hi Greg,
As the POSIX conversion part is already answered, I'll add:

as.Date("16/03/2021",format="%d/%m/%Y")

converts to Date object and

axis.Date()

will display dates on the date axis in base graphics.

Jim

On Tue, 16 Mar 2021, 08:33 Gregory Coats via R-help <[hidden email]
wrote:

> I store in a text file the dates and times that an event occurred.
> How do I direct R to import these text characters, and store the year,
> month, day, hour, minute, and second as a date?
> How do I then plot this series of dates?
> 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
> 2021-03-13 13:51:12
> 2021-03-13 18:03:13
> 2021-03-13 22:20:28
> 2021-03-14 08:59:03
> 2021-03-14 13:15:56
> 2021-03-14 17:25:23
> 2021-03-14 21:36:26
> Greg Coats
> [hidden email]
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

R help mailing list-2
In reply to this post by R help mailing list-2
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

> library(ggplot2)
> # Read a txt file on the Desktop, named "myDat.txt"
> myDat <- read.delim("~/Desktop/myDat.txt", header = TRUE, sep = ",")
> head(myDat)
  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
> # convert data to date time object
> myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format = "%Y-%M-%d %H:%M:%OS")
Error in `$<-.data.frame`(`*tmp*`, datetime, value = numeric(0)) :
  replacement has 0 rows, data has 13
>
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

Sarah Goslee
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
<[hidden email]> wrote:

>
> 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
>
> > library(ggplot2)
> > # Read a txt file on the Desktop, named "myDat.txt"
> > myDat <- read.delim("~/Desktop/myDat.txt", header = TRUE, sep = ",")
> > head(myDat)
>   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
> > # convert data to date time object
> > myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format = "%Y-%M-%d %H:%M:%OS")
> Error in `$<-.data.frame`(`*tmp*`, datetime, value = numeric(0)) :
>   replacement has 0 rows, data has 13
> >
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



--
Sarah Goslee (she/her)
http://www.numberwright.com

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

R help mailing list-2
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
> library(ggplot2)
> 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)
> head(myDat)
             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
> myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format ="%Y-%M-%d %H:%M:%OS”)
> ggplot(myDat, aes(x=datetime, y = Y_Var)) + geom_point()
Error in FUN(X[[i]], ...) : object 'Y_Var' not found

> On Mar 16, 2021, at 9:36 AM, Sarah Goslee <[hidden email]> wrote:
>
> 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
> <[hidden email]> wrote:
>>
>> 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
>>
>>> library(ggplot2)
>>> # Read a txt file on the Desktop, named "myDat.txt"
>>> myDat <- read.delim("~/Desktop/myDat.txt", header = TRUE, sep = ",")
>>> head(myDat)
>>  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
>>> # convert data to date time object
>>> myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format = "%Y-%M-%d %H:%M:%OS")
>> Error in `$<-.data.frame`(`*tmp*`, datetime, value = numeric(0)) :
>>  replacement has 0 rows, data has 13
>>>
>>        [[alternative HTML version deleted]]
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> --
> Sarah Goslee (she/her)
> http://www.numberwright.com


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

John Fox
In reply to this post by Sarah Goslee
Dear Greg,

There is no variable named Y_Var in your data set. I suspect that it's
intended to be a generic specification in the recipe you were apparently
given. In fact, there appears to be only one variable in myDat and
that's datetime. What is it that you're trying to do?

A more general comment: If I'm correct and you're just following a
recipe, that's a recipe for problems. You'd probably be more successful
if you tried to learn how ggplot(), etc., work. My apologies if I'm
misinterpreting the source of your difficulties.

I hope this helps,
  John

John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
web: https://socialsciences.mcmaster.ca/jfox/

On 2021-03-16 12:21 p.m., Gregory Coats via R-help 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
>> library(ggplot2)
>> 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)
>> head(myDat)
>               datetime
> 1 2021-03-11 10:00:00
> 2 2021-03-11 14:17
> 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
>> myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format ="%Y-%M-%d %H:%M:%OS”)
>> ggplot(myDat, aes(x=datetime, y = Y_Var)) + geom_point()
> Error in FUN(X[[i]], ...) : object 'Y_Var' not found
>
>> On Mar 16, 2021, at 9:36 AM, Sarah Goslee <[hidden email]> wrote:
>>
>> 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
>> <[hidden email]> wrote:
>>>
>>> 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
>>>
>>>> library(ggplot2)
>>>> # Read a txt file on the Desktop, named "myDat.txt"
>>>> myDat <- read.delim("~/Desktop/myDat.txt", header = TRUE, sep = ",")
>>>> head(myDat)
>>>   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
>>>> # convert data to date time object
>>>> myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format = "%Y-%M-%d %H:%M:%OS")
>>> Error in `$<-.data.frame`(`*tmp*`, datetime, value = numeric(0)) :
>>>   replacement has 0 rows, data has 13
>>>>
>>>         [[alternative HTML version deleted]]
>>> ______________________________________________
>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>> --
>> Sarah Goslee (she/her)
>> http://www.numberwright.com
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

Jeff Newmiller
In reply to this post by R help mailing list-2
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 <[hidden email]> 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
>> library(ggplot2)
>> 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)
>> head(myDat)
>             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
>> myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format
>="%Y-%M-%d %H:%M:%OS”)
>> ggplot(myDat, aes(x=datetime, y = Y_Var)) + geom_point()
>Error in FUN(X[[i]], ...) : object 'Y_Var' not found
>
>> On Mar 16, 2021, at 9:36 AM, Sarah Goslee <[hidden email]>
>wrote:
>>
>> 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
>> <[hidden email]> wrote:
>>>
>>> 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
>>>
>>>> library(ggplot2)
>>>> # Read a txt file on the Desktop, named "myDat.txt"
>>>> myDat <- read.delim("~/Desktop/myDat.txt", header = TRUE, sep =
>",")
>>>> head(myDat)
>>>  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
>>>> # convert data to date time object
>>>> myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format =
>"%Y-%M-%d %H:%M:%OS")
>>> Error in `$<-.data.frame`(`*tmp*`, datetime, value = numeric(0)) :
>>>  replacement has 0 rows, data has 13
>>>>
>>>        [[alternative HTML version deleted]]
>>> ______________________________________________
>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>> --
>> Sarah Goslee (she/her)
>> http://www.numberwright.com
>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

R help mailing list-2
I want to plot the date and time of the event, as reflected in data.
2021-03-11 10:00:00
Greg Coats

> On Mar 16, 2021, at 2:23 PM, Jeff Newmiller <[hidden email]> wrote:
>
> 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 <[hidden email]> 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
>>> library(ggplot2)
>>> 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)
>>> head(myDat)
>>            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
>>> myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format
>> ="%Y-%M-%d %H:%M:%OS”)
>>> ggplot(myDat, aes(x=datetime, y = Y_Var)) + geom_point()
>> Error in FUN(X[[i]], ...) : object 'Y_Var' not found
>>
>>> On Mar 16, 2021, at 9:36 AM, Sarah Goslee <[hidden email]>
>> wrote:
>>>
>>> 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
>>> <[hidden email]> wrote:
>>>>
>>>> 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
>>>>
>>>>> library(ggplot2)
>>>>> # Read a txt file on the Desktop, named "myDat.txt"
>>>>> myDat <- read.delim("~/Desktop/myDat.txt", header = TRUE, sep =
>> ",")
>>>>> head(myDat)
>>>> 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
>>>>> # convert data to date time object
>>>>> myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format =
>> "%Y-%M-%d %H:%M:%OS")
>>>> Error in `$<-.data.frame`(`*tmp*`, datetime, value = numeric(0)) :
>>>> replacement has 0 rows, data has 13
>>>>>
>>>>       [[alternative HTML version deleted]]
>>>> ______________________________________________
>>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>> --
>>> Sarah Goslee (she/her)
>>> http://www.numberwright.com
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> --
> Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

Jeff Newmiller
So what do you want quantity on the y-axis to be?

On March 16, 2021 11:45:32 AM PDT, Gregory Coats <[hidden email]> wrote:

>I want to plot the date and time of the event, as reflected in data.
>2021-03-11 10:00:00
>Greg Coats
>
>> On Mar 16, 2021, at 2:23 PM, Jeff Newmiller
><[hidden email]> wrote:
>>
>> 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
><[hidden email]> 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
>>>> library(ggplot2)
>>>> 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)
>>>> head(myDat)
>>>            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
>>>> myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format
>>> ="%Y-%M-%d %H:%M:%OS”)
>>>> ggplot(myDat, aes(x=datetime, y = Y_Var)) + geom_point()
>>> Error in FUN(X[[i]], ...) : object 'Y_Var' not found
>>>
>>>> On Mar 16, 2021, at 9:36 AM, Sarah Goslee <[hidden email]>
>>> wrote:
>>>>
>>>> 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
>>>> <[hidden email]> wrote:
>>>>>
>>>>> 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
>>>>>
>>>>>> library(ggplot2)
>>>>>> # Read a txt file on the Desktop, named "myDat.txt"
>>>>>> myDat <- read.delim("~/Desktop/myDat.txt", header = TRUE, sep =
>>> ",")
>>>>>> head(myDat)
>>>>> 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
>>>>>> # convert data to date time object
>>>>>> myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format =
>>> "%Y-%M-%d %H:%M:%OS")
>>>>> Error in `$<-.data.frame`(`*tmp*`, datetime, value = numeric(0)) :
>>>>> replacement has 0 rows, data has 13
>>>>>>
>>>>>       [[alternative HTML version deleted]]
>>>>> ______________________________________________
>>>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>> --
>>>> Sarah Goslee (she/her)
>>>> http://www.numberwright.com
>>>
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>> --
>> Sent from my phone. Please excuse my brevity.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

R help mailing list-2
In reply to this post by John Fox
I need a plot that shows the date and time that each event started.
This ggplot command was publicly given to me via this R Help Mailing LIst.
But the result of issuing the ggplot command is an Error in FUN message.
ggplot(myDat, aes(x=datetime, y = Y_Var)) + geom_point()
Error in FUN(X[[i]], ...) : object 'Y_Var' not found
Greg Coats

> On Mar 16, 2021, at 2:18 PM, John Fox <[hidden email]> wrote:
>
> There is no variable named Y_Var in your data set. I suspect that it's intended to be a generic specification in the recipe you were apparently given. In fact, there appears to be only one variable in myDat and that's datetime. What is it that you're trying to do?


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

John Fox
In reply to this post by John Fox
Dear Greg,

Coordinate plots typically have a horizontal (x) and vertical (y) axis.
The command

        ggplot(myDat, aes(x=datetime, y = datetime)) + geom_point()

works, but I doubt that it produces what you want.

You have only one variable in your data set -- datetime -- so it's not
obvious what you want to do. If you can't clearly describe the structure
of the plot you intend to draw, it's doubtful that I or anyone else can
help you.

Best,
  John

John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
web: https://socialsciences.mcmaster.ca/jfox/

On 2021-03-16 2:56 p.m., Gregory Coats via R-help wrote:

> I need a plot that shows the date and time that each event started.
> This ggplot command was publicly given to me via this R Help Mailing LIst.
> But the result of issuing the ggplot command is an Error in FUN message.
> ggplot(myDat, aes(x=datetime, y = Y_Var)) + geom_point()
> Error in FUN(X[[i]], ...) : object 'Y_Var' not found
> Greg Coats
>
>> On Mar 16, 2021, at 2:18 PM, John Fox <[hidden email]> wrote:
>>
>> There is no variable named Y_Var in your data set. I suspect that it's intended to be a generic specification in the recipe you were apparently given. In fact, there appears to be only one variable in myDat and that's datetime. What is it that you're trying to do?
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

Rui Barradas
In reply to this post by Sarah Goslee
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:

> 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
> <[hidden email]> wrote:
>>
>> 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
>>
>>> library(ggplot2)
>>> # Read a txt file on the Desktop, named "myDat.txt"
>>> myDat <- read.delim("~/Desktop/myDat.txt", header = TRUE, sep = ",")
>>> head(myDat)
>>    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
>>> # convert data to date time object
>>> myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format = "%Y-%M-%d %H:%M:%OS")
>> Error in `$<-.data.frame`(`*tmp*`, datetime, value = numeric(0)) :
>>    replacement has 0 rows, data has 13
>>>
>>          [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

Jim Lemon-4
In reply to this post by R help mailing list-2
Hi Greg,
This example may give you a start:

myDat<-read.table(text=
  "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=",",
  stringsAsFactors=FALSE)
myDat$datetime<-strptime(myDat$X,format="%Y-%m-%d %H:%M:%S")
plot(myDat$datetime,rnorm(7),type="b")

Note that I have corrected what I think is a mistake in reading in
your data. The first date has been converted into the field name by
prepending an "X" and then replacing the hyphen and semicolon
characters with periods to coerce it to a name.  I changed that back
to what I think it was and this produces s basic plot with the dates
on the abscissa and some random numbers on the ordinate.

Jim

On Wed, Mar 17, 2021 at 6:26 AM Gregory Coats via R-help
<[hidden email]> wrote:

>
> I need a plot that shows the date and time that each event started.
> This ggplot command was publicly given to me via this R Help Mailing LIst.
> But the result of issuing the ggplot command is an Error in FUN message.
> ggplot(myDat, aes(x=datetime, y = Y_Var)) + geom_point()
> Error in FUN(X[[i]], ...) : object 'Y_Var' not found
> Greg Coats
>
> > On Mar 16, 2021, at 2:18 PM, John Fox <[hidden email]> wrote:
> >
> > There is no variable named Y_Var in your data set. I suspect that it's intended to be a generic specification in the recipe you were apparently given. In fact, there appears to be only one variable in myDat and that's datetime. What is it that you're trying to do?
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

R help mailing list-2
In reply to this post by John Fox
Thank you. Let me redefine the situation.
Each time an event starts, I record the date and time.
Each day there are 4 new events. Time is the only variable.
I would like to graphically show how the time for events 1, 2, 3, and 4 for the current day compare to the times for events 1, 2, 3, and 4 for the previous day. How would I plot / display those times differences?
Greg Coats

library (ggplot2)
myDat <- read.table(text =
"datetime
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
2021-03-13 13:51:12
2021-03-13 18:03:13
2021-03-13 22:20:28
2021-03-14 08:59:03
2021-03-14 13:15:56
2021-03-14 17:25:23
2021-03-14 21:36:26",
sep = ",", header = TRUE)
head(myDat)
myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format ="%Y-%M-%d %H:%M:%OS")

> On Mar 16, 2021, at 3:34 PM, John Fox <[hidden email]> wrote:
>
> Dear Greg,
>
> Coordinate plots typically have a horizontal (x) and vertical (y) axis. The command
>
> ggplot(myDat, aes(x=datetime, y = datetime)) + geom_point()
>
> works, but I doubt that it produces what you want.
>
> You have only one variable in your data set -- datetime -- so it's not obvious what you want to do. If you can't clearly describe the structure of the plot you intend to draw, it's doubtful that I or anyone else can help you.
>
> Best,
> John
>
> John Fox, Professor Emeritus
> McMaster University
> Hamilton, Ontario, Canada
> web: https://socialsciences.mcmaster.ca/jfox/ <https://socialsciences.mcmaster.ca/jfox/>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

Daniel Nordlund-3
On 3/16/2021 3:32 PM, Gregory Coats via R-help wrote:

> Thank you. Let me redefine the situation.
> Each time an event starts, I record the date and time.
> Each day there are 4 new events. Time is the only variable.
> I would like to graphically show how the time for events 1, 2, 3, and 4 for the current day compare to the times for events 1, 2, 3, and 4 for the previous day. How would I plot / display those times differences?
> Greg Coats
>
> library (ggplot2)
> myDat <- read.table(text =
> "datetime
> 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
> 2021-03-13 13:51:12
> 2021-03-13 18:03:13
> 2021-03-13 22:20:28
> 2021-03-14 08:59:03
> 2021-03-14 13:15:56
> 2021-03-14 17:25:23
> 2021-03-14 21:36:26",
> sep = ",", header = TRUE)
> head(myDat)
> myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format ="%Y-%M-%d %H:%M:%OS")
>
>> On Mar 16, 2021, at 3:34 PM, John Fox <[hidden email]> wrote:
>>
>> Dear Greg,
>>
>> Coordinate plots typically have a horizontal (x) and vertical (y) axis. The command
>>
>> ggplot(myDat, aes(x=datetime, y = datetime)) + geom_point()
>>
>> works, but I doubt that it produces what you want.
>>
>> You have only one variable in your data set -- datetime -- so it's not obvious what you want to do. If you can't clearly describe the structure of the plot you intend to draw, it's doubtful that I or anyone else can help you.
>>
>> Best,
>> John
>>
>> John Fox, Professor Emeritus
>> McMaster University
>> Hamilton, Ontario, Canada
>> web: https://socialsciences.mcmaster.ca/jfox/ <https://socialsciences.mcmaster.ca/jfox/>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

I am only guessing about what you are looking for by way of a visual
comparison.  Here is one way of visualizing the variation of event times

library (ggplot2)
myDat <- read.table(text =
"datetime
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
2021-03-13 13:51:12
2021-03-13 18:03:13
2021-03-13 22:20:28
2021-03-14 08:59:03
2021-03-14 13:15:56
2021-03-14 17:25:23
2021-03-14 21:36:26",
sep = ",", header = TRUE)

# create datepart variable - dte
myDat$dte <- as.Date(myDat$datetime, "%m/%d/%Y", tz='')

# create timepart variable - tme
library(lubridate)
myDat$tme <- hms::hms(as.numeric(myDat$datetime -
floor_date(myDat$datetime, "1 day"), unit="secs"))

# plot event date by time grouped by date
ggplot(myDat, aes(x=tme, y = dte, group = dte)) + geom_line() +
geom_point() +
       expand_limits(y=as.Date(c('20210301', '20210331'),'%Y%m%d'))

If this doesn't help get you started, you need to provide a description
of what you want your plot to look like.

Dan

--
Daniel Nordlund
Port Townsend, WA  USA

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

R help mailing list-2
In reply to this post by R help mailing list-2
It sounds to me like you want to take your data and extract one column for
JUST the date and another column for just some measure of the time, such as
the number of seconds since midnight or hours in a decimal format where
12:45 PM might be 12.75.

You now can graph date along the X axis and time along the Y (or vice versa)
and show the result in one of many ways as points or horizontal line
segments or whatever works for you.

If you always had 4 time measures for each day, or did some work, you might
also have a column for which time that is for a day, a number ranging from
one to 4 and this column could be used to set the color or other attribute
if that was useful.

So, to review. Your one data item need not be kept as the only column and if
you make more columns, you might have something to graph.

Here is an example that worked for me doing roughly what I mentioned but
note my names changed. It makes two plots.

library (ggplot2)
myDat <- read.table(text =
                      "datetimeraw
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
2021-03-13 13:51:12
2021-03-13 18:03:13
2021-03-13 22:20:28
2021-03-14 08:59:03
2021-03-14 13:15:56
2021-03-14 17:25:23
2021-03-14 21:36:26",
                    sep = ",", header = TRUE)
head(myDat)
myDat$datetime <- as.POSIXct(myDat$datetimeraw, tz = "", format ="%Y-%M-%d
%H:%M:%OS")
myDat$date <- factor(format(myDat$datetime, "%Y-%m-%d"))
myDat$time <- format(myDat$datetime, "%H:%M")
myDat$seq <- factor(rep(1:4, 3))

# just dots
ggplot(data=myDat,aes(x=date, y=time)) + geom_point(aes(color=seq))

# Also text
ggplot(data=myDat,aes(x=date, y=time, label=time)) +
  geom_point(aes(color=seq)) +
               geom_text(aes(color=seq))



-----Original Message-----
From: R-help <[hidden email]> On Behalf Of Gregory Coats via
R-help
Sent: Tuesday, March 16, 2021 6:32 PM
To: John Fox <[hidden email]>
Cc: r-help mailing list <[hidden email]>
Subject: Re: [R] How to plot dates

Thank you. Let me redefine the situation.
Each time an event starts, I record the date and time.
Each day there are 4 new events. Time is the only variable.
I would like to graphically show how the time for events 1, 2, 3, and 4 for
the current day compare to the times for events 1, 2, 3, and 4 for the
previous day. How would I plot / display those times differences?
Greg Coats

library (ggplot2)
myDat <- read.table(text =
"datetime
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
2021-03-13 13:51:12
2021-03-13 18:03:13
2021-03-13 22:20:28
2021-03-14 08:59:03
2021-03-14 13:15:56
2021-03-14 17:25:23
2021-03-14 21:36:26",
sep = ",", header = TRUE)
head(myDat)
myDat$datetime <- as.POSIXct(myDat$datetime, tz = "", format ="%Y-%M-%d
%H:%M:%OS")

> On Mar 16, 2021, at 3:34 PM, John Fox <[hidden email]> wrote:
>
> Dear Greg,
>
> Coordinate plots typically have a horizontal (x) and vertical (y)
> axis. The command
>
> ggplot(myDat, aes(x=datetime, y = datetime)) + geom_point()
>
> works, but I doubt that it produces what you want.
>
> You have only one variable in your data set -- datetime -- so it's not
obvious what you want to do. If you can't clearly describe the structure of
the plot you intend to draw, it's doubtful that I or anyone else can help
you.
>
> Best,
> John
>
> John Fox, Professor Emeritus
> McMaster University
> Hamilton, Ontario, Canada
> web: https://socialsciences.mcmaster.ca/jfox/ 
> <https://socialsciences.mcmaster.ca/jfox/>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

R help mailing list-2
In reply to this post by Daniel Nordlund-3
Dan, Thank you for this guidance.
Unfortunately, I do not have the library lubridate, and I do not at this moment know where to go to get this library for an Apple MacBook.

> library(lubridate)
Error in library(lubridate) : there is no package called ‘lubridate’

Greg Coats
Reston, Virginia USA

> On Mar 16, 2021, at 7:49 PM, Daniel Nordlund <[hidden email]> wrote:
>
> # create timepart variable - tme
> library(lubridate)


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

R help mailing list-2
In reply to this post by R help mailing list-2
Thank you very much.
In addition to what your did, for event 1, I would like to draw a horizontal line connecting from day 1 to day 2 to day 3 to day 4.
Then, for event 2, I would like to draw a horizontal line connecting from day 1 to day 2 to day 3 to day 4.
Similarly for events 3, and 4. Is that convenient to do?
Greg Coats

> On Mar 16, 2021, at 8:01 PM, Avi Gross via R-help <[hidden email]> wrote:
>
> Here is an example that worked for me doing roughly what I mentioned but
> note my names changed. It makes two plots.
>
> library (ggplot2)
> myDat <- read.table(text =
>                      "datetimeraw
> 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
> 2021-03-13 13:51:12
> 2021-03-13 18:03:13
> 2021-03-13 22:20:28
> 2021-03-14 08:59:03
> 2021-03-14 13:15:56
> 2021-03-14 17:25:23
> 2021-03-14 21:36:26",
>                    sep = ",", header = TRUE)
> head(myDat)
> myDat$datetime <- as.POSIXct(myDat$datetimeraw, tz = "", format ="%Y-%M-%d
> %H:%M:%OS")
> myDat$date <- factor(format(myDat$datetime, "%Y-%m-%d"))
> myDat$time <- format(myDat$datetime, "%H:%M")
> myDat$seq <- factor(rep(1:4, 3))
>
> # just dots
> ggplot(data=myDat,aes(x=date, y=time)) + geom_point(aes(color=seq))
>
> # Also text
> ggplot(data=myDat,aes(x=date, y=time, label=time)) +
>  geom_point(aes(color=seq)) +
>               geom_text(aes(color=seq))

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to plot dates

R help mailing list-2
Not sure what you mean by a horizontal line, Greg.

I change one of my plots to add a path between corresponding values of the
sequence variable but obviously those lines are mostly not horizontal. Look
at geom_path and similar geoms like geom_line to connect endpoints grouped
whatever way you specify.

ggplot(data=myDat,aes(x=date, y=time, label=time)) +
  geom_point(aes(color=seq)) +
  geom_path(aes(group=seq)) +
  geom_text(aes(color=seq))

As this is a help group, I think I am done.

From: Gregory Coats <mailto:[hidden email]>
Sent: Tuesday, March 16, 2021 8:26 PM
To: Avi Gross <mailto:[hidden email]>
Cc: mailto:[hidden email]
Subject: Re: [R] How to plot dates

Thank you very much.
In addition to what your did, for event 1, I would like to draw a horizontal
line connecting from day 1 to day 2 to day 3 to day 4.
Then, for event 2, I would like to draw a horizontal line connecting from
day 1 to day 2 to day 3 to day 4.
Similarly for events 3, and 4. Is that convenient to do?
Greg Coats

On Mar 16, 2021, at 8:01 PM, Avi Gross via R-help
<mailto:[hidden email]> wrote:

Here is an example that worked for me doing roughly what I mentioned but
note my names changed. It makes two plots.

library (ggplot2)
myDat <- read.table(text =
                     "datetimeraw
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
2021-03-13 13:51:12
2021-03-13 18:03:13
2021-03-13 22:20:28
2021-03-14 08:59:03
2021-03-14 13:15:56
2021-03-14 17:25:23
2021-03-14 21:36:26",
                   sep = ",", header = TRUE)
head(myDat)
myDat$datetime <- as.POSIXct(myDat$datetimeraw, tz = "", format ="%Y-%M-%d
%H:%M:%OS")
myDat$date <- factor(format(myDat$datetime, "%Y-%m-%d"))
myDat$time <- format(myDat$datetime, "%H:%M")
myDat$seq <- factor(rep(1:4, 3))

# just dots
ggplot(data=myDat,aes(x=date, y=time)) + geom_point(aes(color=seq))

# Also text
ggplot(data=myDat,aes(x=date, y=time, label=time)) + 
 geom_point(aes(color=seq)) + 
              geom_text(aes(color=seq))


From: Gregory Coats <[hidden email]>
Sent: Tuesday, March 16, 2021 8:26 PM
To: Avi Gross <[hidden email]>
Cc: [hidden email]
Subject: Re: [R] How to plot dates

Thank you very much.
In addition to what your did, for event 1, I would like to draw a horizontal
line connecting from day 1 to day 2 to day 3 to day 4.
Then, for event 2, I would like to draw a horizontal line connecting from
day 1 to day 2 to day 3 to day 4.
Similarly for events 3, and 4. Is that convenient to do?
Greg Coats


On Mar 16, 2021, at 8:01 PM, Avi Gross via R-help
<mailto:[hidden email]> wrote:

Here is an example that worked for me doing roughly what I mentioned but
note my names changed. It makes two plots.

library (ggplot2)
myDat <- read.table(text =
                     "datetimeraw
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
2021-03-13 13:51:12
2021-03-13 18:03:13
2021-03-13 22:20:28
2021-03-14 08:59:03
2021-03-14 13:15:56
2021-03-14 17:25:23
2021-03-14 21:36:26",
                   sep = ",", header = TRUE)
head(myDat)
myDat$datetime <- as.POSIXct(myDat$datetimeraw, tz = "", format ="%Y-%M-%d
%H:%M:%OS")
myDat$date <- factor(format(myDat$datetime, "%Y-%m-%d"))
myDat$time <- format(myDat$datetime, "%H:%M")
myDat$seq <- factor(rep(1:4, 3))

# just dots
ggplot(data=myDat,aes(x=date, y=time)) + geom_point(aes(color=seq))

# Also text
ggplot(data=myDat,aes(x=date, y=time, label=time)) + 
 geom_point(aes(color=seq)) + 
              geom_text(aes(color=seq))

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
12