-1

I have the next plot:

enter image description here

The code for getting it is this:

Plot<- ggplot(foo,aes(x=Time, y=value, colour=Axis)) + 
  geom_line(size=1) +
  theme_bw() + 
  labs(x=expression(Time~(HMS)),y=expression(Raw~acceleration~(g))) + 
  theme(strip.background=element_blank(),
        axis.title.x =element_text(margin = margin(t = 10, r = 0, b = 0, l = 0),size = 16,face = "bold"),
        axis.title.y =element_text(margin = margin(t = 0, r = 10, b = 0, l = 0),size = 16,face = "bold"),
        axis.text.x = element_text(angle = 0, hjust = 0.5,size = 15),
        axis.text.y = element_text(angle = 0, hjust = 0.5,size = 15),
        axis.line = element_line(),
        panel.grid.major= element_blank(),
        panel.grid.minor = element_blank(),
        legend.text=element_text(size=14),
        legend.title = element_text(size=15, face = "bold"),
        legend.key=element_blank(),
        legend.position = "right",
        panel.border = element_blank(),
        strip.placement = "outside") +
  guides(color=guide_legend(override.aes=list(fill=NA))) +
  scale_x_datetime(labels = date_format("%H:%M:%S"),
    breaks=date_breaks("10 sec")) +
  scale_y_continuous(breaks = seq(-4,3,1))
Plot

I am trying to add letters within the plot using annotate(), however I don't know how to get it since the format of the x-axis is %Y-%m-%d %H:%M:%S.

Plot  + annotate("text", x=as.POSIXct("15:40:00"), y=-3.5, label= "A")
Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

Does anyone know how to do it?

Thanks in advance

Dekike
  • 1,050
  • 3
  • 11

1 Answers1

0

I found the way. I just created an object with the x position in which I wanted the letter. In my case, for example, I wanted the letter to be around the time 15:40:05, so I did this:

Time1 <- foo$Time[foo$Time>"2020-03-21 16:40:05.000" & foo$Time<"2020-03-21 16:40:05.100" & foo$Axis=="surge"]

Once I had that, I just had to refer to Time1 in annotate():

Plot  + annotate("text", x=Time1, y=-3.5, label= "A")

enter image description here

Dekike
  • 1,050
  • 3
  • 11