[R] How to plot dates
Avi Gross
@v|gro@@ @end|ng |rom ver|zon@net
Wed Mar 17 01:01:25 CET 2021
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 <r-help-bounces using r-project.org> On Behalf Of Gregory Coats via
R-help
Sent: Tuesday, March 16, 2021 6:32 PM
To: John Fox <jfox using mcmaster.ca>
Cc: r-help mailing list <r-help using r-project.org>
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 <jfox using mcmaster.ca> 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]]
______________________________________________
R-help using r-project.org 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.
More information about the R-help
mailing list