简体   繁体   中英

Adding a label to a straight line in each facet of a facet wrapped ggplot

I've been struggling with one last bit of code to make this graph I'm working on really work for me and my audience. I have a bar chart with a two lines (one is acting as a rolling average, the other as the peak of that rolling average). What I want to do is label that peak line with a number, one time, but in each facet where the number is different in each facet. Here's some stripped down data and code:

tdf <- data.frame(a=as.POSIXct(c("2019-10-15 08:00:00","2019-10-15 09:00:00","2019-10-15 10:00:00","2019-10-15 08:00:00","2019-10-15 09:00:00","2019-10-15 10:00:00")),
                  b=as.Date(c("2019-09-02","2019-09-02","2019-09-02","2019-09-03","2019-09-03","2019-09-03")),
                  m1=c(0.2222222,0.3636364, 0.2307692, 0.4000000, 0.3428571, 0.3529412),
                  m2=c(0.2222222,0.2929293, 0.2972028, 0.3153846, 0.3714286, 0.3529412),
                  m3=c(0.2929293, 0.2929293, 0.2929293, 0.3529412,0.3529412,0.3529412))
 g <- ggplot(data = tdf, aes(x = a, y = m1)) +
  geom_bar(stat = "identity", alpha = 0.75, fill = 352) +
  xlab("time of day") +
  ylab("metric name") +
  ggtitle("Graph Title") +
  scale_x_datetime(breaks = scales::date_breaks("1 hours"), 
                   date_labels = "%H")+
  scale_y_continuous(breaks = c(0,.10,.20,.30,.40,.50,.50,.60,.70,.80,.90,1.0), 
                     labels = scales::percent) +
  theme_minimal()
# add line for m2
g <- g +
  geom_line(data = tdf, 
            aes(x = a, y = m2), 
            color = "blue", 
            size = 1.2)
# add line for m3
g <- g + geom_line(data=tdf, 
                   aes(x = a, y = m3), 
                   color = "#d95f02", 
                   size = 0.6, 
                   linetype = "dashed")
# last attempt to label the line results in an error: Invalid input: time_trans works with objects of class POSIXct
#g <- g+geom_text(aes(x=-Inf, y=Inf, label=median(tdf$m3)), size=2, hjust=-0.5, vjust= 1.4,inherit.aes=FALSE)
# facet wrap
g <- g + facet_wrap(~b, ncol = 5, scales = "fixed")

I've seen a few techniques, but none of them seem to relate having a time for the x-axis in the facets, and each facet having a different date. I'm reasonably certain it's related to the date, but I sort of have no clue how to make the text block happen on each facet anyway.

You just need to pass a different dataset to the labeling layer that still preserves your faceting variable. This will work using dplyr

g <- g + 
  geom_text(data = tdf %>% 
              group_by(b) %>% 
              summarize(median = median(m3)), 
            aes(x = as.POSIXct(-Inf, origin="1970-01-01"), 
                y = Inf, 
                label = median), 
            size = 2,
            hjust = -0.5,
            vjust = 1.4, 
            inherit.aes = FALSE)

We also have to explicitly convert the x to a date/time value for the axis to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM