Priit Mets
Priit Mets

Reputation: 495

How insert labels to the ggplot plot?

I have a plot, where dashed red lines separated based on the highest value of week_hour for specific weekday2 (week_hour_dummy was created for dashed line). I want that between two red dashed lines was an entry that corresponds to weekday2 An approximate view of the new plot is attached below. (if not possible to do so, is it possible to add weekday2 to X-axis instead?)

q3<-structure(list(week_hour = 1:168, week_hour_dummy = c(0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 144, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 168), over_supply = c(15.6, 17.2, 13, 11.6, 9.8, 13.2, 16.8, 
15.8, 13.8, 12.8, 26.4, 42.2, 40.8, 38, 41.2, 48.2, 47.2, 42.4, 
18.4, 35.4, 47.2, 42, 26.6, 24.8, 19.4, 21.2, 16.2, 16.6, 16.8, 
16.2, 21.8, 23.4, 13, 13, 36.2, 42.2, 41, 38.4, 39.6, 47, 43.2, 
42.4, 21.8, 23.8, 40.2, 34.6, 23.8, 15, 15.8, 23, 22.4, 15.8, 
15.6, 18.5, 21.4, 24.4, 18.8, 15.4, 46.8, 53.4, 43.6, 42.4, 48.6, 
48, 57, 19.2, 14.8, 23.2, 39.2, 43.8, 28, 13, 15.4, 28.6, 16.6, 
15.6, 18.4, 20.2, 25, 22.8, 14.2, 10.8, 31.4, 37.6, 37.2, 32, 
37.4, 40.4, 40.4, 23, 8.6, 11, 23.2, 33, 26.2, 8.2, 7, 22.4, 
20, 12, 15.4, 18.8, 22.6, 20.4, 10.2, 4.4, 21.2, 28, 30.6, 29.4, 
26, 22.8, 31.4, 20.4, 4.8, 18.6, 27.2, 35, 48.8, 32.6, 20.6, 
19.8, 12.6, 7.4, 11, 23.8, 25.2, 21.4, 20.4, 20.2, 17.6, 18, 
20.2, 30.4, 22.6, 25.8, 31.8, 35.6, 43, 37, 36.8, 44, 47.4, 33.2, 
17, 16.8, 22.6, 15.8, 15, 23, 23.8, 21.8, 17.4, 17.4, 21.2, 18.2, 
11, 14.6, 26.6, 31, 27.4, 29.4, 27.4, 29.6, 33, 34.8, 16, 17.4
), weekday2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("E", 
"T", "K", "N", "R", "L", "P"), class = "factor")), row.names = c(NA, 
-168L), class = c("tbl_df", "tbl", "data.frame"))

ggplot(data=q3) +
  geom_line(aes(x=week_hour  , y=over_supply ,group=1,fill="Over-supply"),lwd=1,col="blue")+
  geom_vline(data = subset(q3, !(week_hour_dummy == 0)), # filter data source
             aes(xintercept = week_hour ),
             size = 1, colour = "red", linetype="dashed")

enter image description here

Upvotes: 0

Views: 139

Answers (1)

Peter
Peter

Reputation: 12739

This may help:

Add geom_text() to create the label working with a version of your data filtered on the mid point between each 24 hour period; you can adjust label appearance and location arguments to suit your requirements.


library(ggplot2)
library(dplyr)

  
ggplot(q3) +
  geom_line(aes(x = week_hour, y = over_supply),lwd = 1, col = "blue")+
  geom_text(data = filter(q3, week_hour %% 24 == 12),
            aes(x = week_hour, y = Inf, label = weekday2),
            colour = "red",
            size = 10,
            vjust = 1.2)+
  geom_vline(data = subset(q3, !(week_hour_dummy == 0)), # filter data source
             aes(xintercept = week_hour ),
             size = 1, colour = "red", linetype="dashed")

Created on 2021-07-06 by the reprex package (v2.0.0)

Upvotes: 1

Related Questions