Manu
Manu

Reputation: 1120

Can't place an annotation on ggplot - R

I have the following dataset:

library(tidyverse)
the_dates <- c("2020-08-01","2020-08-02","2020-08-03","2020-08-04")
the_dates <- as.Date(the_dates)
solno <- c(3,4,2,3)

I try to create a line chart with an annotation. Since I don't know exactly what will be values to place the annotation, I tried the Inf trick:

dataex <- data.frame(the_dates, solno)
dataex %>% ggplot(aes(the_dates, solno)) + geom_line() +
  annotate("text",x = -Inf, y= Inf, label = "See me")

But I get this error message Error: Invalid input: date_trans works with objects of class Date only

I don't know what I am doing wrong. Please, any help will be greatly appreciated.

Upvotes: 0

Views: 640

Answers (1)

Duck
Duck

Reputation: 39595

Your issue is that x-axis is date class. So try this code. As there is not infinite values for dates you can play aroun minimum values in your variable the_dates. Here the code:

#Data
dataex <- data.frame(the_dates, solno)
#Code
dataex %>% ggplot(aes(the_dates, solno)) + geom_line() +
  annotate("text",x = min(dataex$the_dates), y= Inf, label = "See me",hjust=-0.1,vjust=1)

Output:

enter image description here

Upvotes: 3

Related Questions