[R] How to plot dates
John Fox
j|ox @end|ng |rom mcm@@ter@c@
Tue Mar 16 19:18:33 CET 2021
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 <sarah.goslee using gmail.com> 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
>> <r-help using r-project.org> 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]]
>>> ______________________________________________
>>> 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.
>> --
>> Sarah Goslee (she/her)
>> http://www.numberwright.com
>
>
> [[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