[R] Print date on y axis with month, day, and year

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Fri May 10 12:12:30 CEST 2024


Às 00:58 de 10/05/2024, Sorkin, John escreveu:
> I am trying to use ggplot to plot the data, and R code, below. The dates (jdate) are printing as Mar 01, Mar 15, etc. I want to have the date printed as MMM DD YYYY (or any other way that will show month, date, and year, e.g. mm/dd/yy). How can I accomplish this?
> 
> yyy  <- structure(list(
>    jdate = structure(c(19052, 19053, 19054, 19055,
>                        19058, 19059, 19060, 19061, 19062, 19063, 19065, 19066, 19067,
>                        19068, 19069, 19072, 19073, 19074, 19075, 19076, 19077, 19083,
>                        19086, 19087, 19088, 19089, 19090, 19093, 19094, 19095), class = "Date"),
>      Sum = c ( 1,  3,  9, 11, 13, 16, 18, 22, 26, 27, 30, 32, 35, 39,  41,
>               43, 48, 51, 56, 58, 59, 63, 73, 79, 81, 88, 91, 93, 96, 103)),
>      row.names = c(NA, 30L), class = "data.frame")
> yyy
> class(yyy$jdate)
> ggplot(data=yyy[1:30,],aes(as.Date(jdate,format="%m-%d-%Y"),Sum)) +geom_point()
> 
> 
> Thank you
> John
> 
> 
> 
> John David Sorkin M.D., Ph.D.
> Professor of Medicine, University of Maryland School of Medicine;
> Associate Director for Biostatistics and Informatics, Baltimore VA Medical Center Geriatrics Research, Education, and Clinical Center;
> PI Biostatistics and Informatics Core, University of Maryland School of Medicine Claude D. Pepper Older Americans Independence Center;
> Senior Statistician University of Maryland Center for Vascular Research;
> 
> Division of Gerontology and Paliative Care,
> 10 North Greene Street
> GRECC (BT/18/GR)
> Baltimore, MD 21201-1524
> Cell phone 443-418-5382
> 
> 
> 
> ______________________________________________
> 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.
Hello,

Since class(yyy$jdate) returns "Date", you have a real date and 
scale_x_date can handle the printed formats, there is no need for an 
extra as.Date in aes(). And get rid of the format = "%m-%d-%Y" argument.

Let scale_x_date take care of formating the date as you want it 
displayed. Any of the two below is a valid date format.



ggplot(data = yyy[1:30,], aes(jdate, Sum)) +
    geom_point() +
    # scale_x_date(date_labels = "%b %d, %Y")
    scale_x_date(date_labels = "%m/%d/%Y")



Hope this helps,

Rui Barradas



-- 
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença de vírus.
www.avg.com



More information about the R-help mailing list