[R] How to plot dates
djnord|und @end|ng |rom gm@||@com
Wed Mar 17 00:49:26 CET 2021
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 =
> 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)
> 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.
>> 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
> 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
myDat <- read.table(text =
sep = ",", header = TRUE)
# create datepart variable - dte
myDat$dte <- as.Date(myDat$datetime, "%m/%d/%Y", tz='')
# create timepart variable - tme
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() +
If this doesn't help get you started, you need to provide a description
of what you want your plot to look like.
Port Townsend, WA USA
More information about the R-help