How can I persuade ggplot2 geom_text to label a specified date in a time series plot?

前端 未结 1 1896
清酒与你
清酒与你 2020-12-28 19:32

I am using ggplot2 to plot simple line charts of time series data. One difficulty I have run into is labelling specific points corresponding to x-axis values i.e. dates.

相关标签:
1条回答
  • 2020-12-28 19:48

    To add text to ggplot, use geom_text:

    Method 1: Add a column of labels to your data.frame:

    tmp$note <- LETTERS[1:7]
    
    ggplot(tmp,aes(date, price, label=note)) +
      geom_line() +
      geom_text(vjust=0, colour="red")
    

    enter image description here

    Method 2: Add a specific label:

    ggplot(tmp,aes(date, price, label=date)) +
      geom_line() +
      geom_text(data=tmp[3, ], label="Something of note", vjust=1)
    

    enter image description here

    0 讨论(0)
提交回复
热议问题