Skip to content

How to plot dates

3 messages · John Fox, Gregory Coats, Jim Lemon

#
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:
#
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

  
  
#
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
<r-help at r-project.org> wrote: