0

Let's assume I have the following dataframe:

df = data.frame(Date = seq(as.Date("2001-01-01"), as.Date("2020-12-31"), by = "day"),
          Data = randn(length(seq(as.Date("2001-01-01"), as.Date("2020-12-31"), by = "day")),1),
          stringsAsFactors = F)

and a vector of words:

key_words = c("ciao", "bye", "thanks", "for", "your", "help", "SO", "is", "amazing", "really")

I would like to plot it with ggplot by adding, in the second y-axis the vector of words key_words, similarly to the figure below:

enter image description here

This is the simple code to plot the graph in ggplot:

ggplot(df) + geom_line(aes(x = Date, y = Data), col = "blue") + labs(y = "", x = "") + theme_classic()

Is there anyone who can help me add key_words as a column in the figure above?

Thanks!

Rollo99
  • 1,601
  • 7
  • 15
  • 2
    Maybe https://stackoverflow.com/questions/65744248/how-to-add-a-free-text-entry-as-a-legend-to-ggplot is what your are looking for? – stefan Jan 17 '21 at 09:53
  • 1
    @stefan thanks a lot! I typed anything but that title :D thanks again – Rollo99 Jan 17 '21 at 10:00

1 Answers1

1

In case you want to link the key words place with the fist y axis, try this :

ggplot(df) + geom_line(aes(x = Date, y = Data), col = "blue") + labs(y = "", x = "") +
  scale_y_continuous(limits=c(-4, 5), breaks = -5:5,
                      , sec.axis = sec_axis( ~.  , breaks = -4:5,  labels = key_words, 
                                             name = "name of the new axis")
                     )
                     +
  theme_classic()