Skip to content

Date order question

6 messages · Thomas Subia, Andrew Simmons, Ebert,Timothy Aaron +3 more

#
Colleagues,

date<-c("12-29","12-30","01-01")
PT <- c(.106,.130,.121)
data <- data.frame(date,PT)
ggplot(data, aes(x=date,y=PT,group=1))+
  geom_point(size=4)+
  geom_line()+
  geom_hline(yintercept =c(1,.60,0,.30,.25,.2))+
  scale_y_continuous(label=scales::label_percent(),breaks=c(1,0.6,0,.3,0.25,0.2))+
  annotate("text", x=2.5, y=.1, label="Very Good",size=5,fontface="bold")+
  annotate("text", x=2.5, y=.225, label="Good",size=5,fontface="bold")+
  annotate("text", x=2.5, y=.28, label="Marginal",size=5,fontface="bold") +
  annotate("text", x=2.5, y=.45, label="Inadequate",size=6,fontface="bold")+
  annotate("text", x=2.5, y=.8, label="OOC",size=6,fontface="bold")+
  annotate("text", x=2.5, y=-.05, label="PT Not Done",size=5,fontface="bold")+
  theme_cowplot()

The plot has the wrong date order.
What is desired is 12-29, 12-30 and 01-01.

Some feedback would be appreciated.

All the best,
Thomas Subia

"De quoi devenir chevre? Des donnees"
#
I converted `date` to a factor and it seemed to work:


```
library(ggplot2)
library(cowplot)
date <- c("12-29","12-30","01-01")
date <- factor(date, labels = unique(date))
PT <- c(.106,.130,.121)
data <- data.frame(date,PT)
ggplot(data, aes(x=date,y=PT,group=1))+
  geom_point(size=4)+
  geom_line()+
  geom_hline(yintercept =c(1,.60,0,.30,.25,.2))+
  scale_y_continuous(label=scales::label_percent(),breaks=c(1,0.6,0,.3,0.25,0.2))+
  annotate("text", x=2.5, y=.1, label="Very Good",size=5,fontface="bold")+
  annotate("text", x=2.5, y=.225, label="Good",size=5,fontface="bold")+
  annotate("text", x=2.5, y=.28, label="Marginal",size=5,fontface="bold") +
  annotate("text", x=2.5, y=.45, label="Inadequate",size=6,fontface="bold")+
  annotate("text", x=2.5, y=.8, label="OOC",size=6,fontface="bold")+
  annotate("text", x=2.5, y=-.05, label="PT Not Done",size=5,fontface="bold")+
  theme_cowplot()
```

On Wed, Jan 4, 2023 at 4:09 PM Thomas Subia
<thomas.subia at fmindustries.com> wrote:
#
1) Your "dates" are not being processed as dates. They are strings.
2) As written your date "01-01" comes first because year is not specified and January comes before December.
3) A library statement is missing. I tried running the program and theme_cowplot() was not found.

As you are plotting strings, you could put a space character in front of the December dates so that they are first. 
date<-c(" 12-29"," 12-30","01-01")
That fixes the problem in this example. You can order all the dates by putting more spaces in front of earlier years. That will get messy.


Tim

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Thomas Subia
Sent: Wednesday, January 4, 2023 4:08 PM
To: r-help at r-project.org
Subject: [R] Date order question

[External Email]

Colleagues,

date<-c("12-29","12-30","01-01")
PT <- c(.106,.130,.121)
data <- data.frame(date,PT)
ggplot(data, aes(x=date,y=PT,group=1))+
  geom_point(size=4)+
  geom_line()+
  geom_hline(yintercept =c(1,.60,0,.30,.25,.2))+
  scale_y_continuous(label=scales::label_percent(),breaks=c(1,0.6,0,.3,0.25,0.2))+
  annotate("text", x=2.5, y=.1, label="Very Good",size=5,fontface="bold")+
  annotate("text", x=2.5, y=.225, label="Good",size=5,fontface="bold")+
  annotate("text", x=2.5, y=.28, label="Marginal",size=5,fontface="bold") +
  annotate("text", x=2.5, y=.45, label="Inadequate",size=6,fontface="bold")+
  annotate("text", x=2.5, y=.8, label="OOC",size=6,fontface="bold")+
  annotate("text", x=2.5, y=-.05, label="PT Not Done",size=5,fontface="bold")+
  theme_cowplot()

The plot has the wrong date order.
What is desired is 12-29, 12-30 and 01-01.

Some feedback would be appreciated.

All the best,
Thomas Subia

"De quoi devenir chevre? Des donnees"

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu%7C9f58ce4e64d04d2b3f0908daee97dcb5%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638084633565926697%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=y7Co7Ae8RJG4AWxjwLLNcvopfUt7fGRBguUovhYaxl8%3D&reserved=0
PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7C9f58ce4e64d04d2b3f0908daee97dcb5%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638084633565926697%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=pTYhbgrmoAu6g3%2FDepzJJt45Euazik4D6UJKByOZKuQ%3D&reserved=0
and provide commented, minimal, self-contained, reproducible code.
#
?s 21:08 de 04/01/2023, Thomas Subia escreveu:
Hello,

Transform `date` into a real date column in a dplyr pipe so that the 
original data remains unchanged and plot with scale_x_date which will 
allow for control over the axis breaks and labels.

The only problem are now the text annotations. The x coordinate is 
numeric and it must also be a date object. Below I solve this by 
creating a annotations data set ann_data. In geom_text the data argument 
is now another one, but one layer only annotates all areas.


library(ggplot2)
library(cowplot)
library(dplyr)

date<-c("12-29","12-30","01-01")
PT <- c(.106,.130,.121)
data <- data.frame(date,PT)

ann_data <- data.frame(
   y = c(0.1, 0.225, 0.28, 0.45, 0.8, -0.05),
   text = c("Very Good", "Good", "Marginal", "Inadequate", "OOC", "PT 
Not Done")
)
ann_data$x <- as.Date("2022-12-31")


data %>%
   mutate(newyear = ifelse(substr(date, 1, 2) > "01", "2022-", "2023-"),
          date = as.Date(paste0(newyear, date))) %>%
   ggplot(aes(x = date, y = PT)) +
   geom_point(size = 4) +
   geom_line() +
   geom_hline(yintercept = c(1,.60,0,.30,.25,.2)) +
   #
   geom_text(
     data = ann_data,
     mapping = aes(x = x, y = y, label = text),
     size = 5, fontface = "bold"
   ) +
   scale_x_date(date_breaks = "1 day", date_labels = "%m-%d") +
   #
 
scale_y_continuous(label=scales::label_percent(),breaks=c(1,0.6,0,.3,0.25,0.2))+
   theme_cowplot()



Hope this helps,

Rui Barradas
#
On Wed, 4 Jan 2023, 21:29 Ebert,Timothy Aaron, <tebert at ufl.edu> wrote:

            
Put the year in front +/- apply as.Date() and you would be fine...

date<-c("2022-12-29","2022-12-30","2023-01-01") |> as.Date()

It may be that the source data doesn't have a year and the example given is
to show us dummy data.  You could 'automate' the addition along the lines
of:

require(tidyverse)
#if you have up to date Tidyverse this includes lubridate
current_date <- sys.Date()
current_month <- month(current_date)
current_year <- year(current_date)
date<-c("12-29","12-30","01-01")
PT <- c(.106,.130,.121)
data <- data.frame(date,PT)
data |>
  # separate the date into month and day column
  separate (date, c("Month", "Day"), sep="-") |>
  # add a year if month is > current month must be last year
 mutate (year = if_else(Month > current_month, current_year - 1,
current_year)) |>
  #rebuild the date
  unite (date, c("Year", "Month", "Day"), sep="-") |>
  mutate(date = as.Date(date)) -> data

If you don't want year on the axis of the graph, that should be dealt with
in ggplot not in the data carpentry
#
Hallo Thomas

Similar as suggested by Rui, you shall change your date to real date e.g. by

library(lubridate)
date <- paste(date, c(rep(2022,2), 2023), sep="-")
date <- mdy(date)

and you need to change also x coordinate in annotate.

ggplot(data, aes(x=date,y=PT,group=1))+
  geom_point(size=4)+
  geom_line()+
  geom_hline(yintercept =c(1,.60,0,.30,.25,.2))+
 
scale_y_continuous(label=scales::label_percent(),breaks=c(1,0.6,0,.3,0.25,0.
2))+
  annotate("text", x=date[2], y=.1, label="Very
Good",size=5,fontface="bold")+
  annotate("text", x=date[2], y=.225, label="Good",size=5,fontface="bold")+
  annotate("text", x=date[2], y=.28,
label="Marginal",size=5,fontface="bold") +
  annotate("text", x=date[2], y=.45,
label="Inadequate",size=6,fontface="bold")+
  annotate("text", x=date[2], y=.8, label="OOC",size=6,fontface="bold")+
  annotate("text", x=date[2], y=-.05, label="PT Not
Done",size=5,fontface="bold")

Cheers
Petr
scale_y_continuous(label=scales::label_percent(),breaks=c(1,0.6,0,.3,0.25,0.
2))
+
label="Inadequate",size=6,fontface="bold")+
Done",size=5,fontface="bold")+
http://www.R-project.org/posting-guide.html