Add box with label in top space in geom_bar

I am working on a facet wrap of bar plots. I have multiple questions, but I'll start with this one. Is there a way to get an adjustable box in the space indicated with red color at top of the facets in the attached image? I want the left and right edges of the box to match the vertical lines that are in the bar plots, which vary by facet item, so ideally the box has to be adjustable. I would like the box to be a solid color and to be able to place a text label in each box.

Here are some data:
dput(df2)
structure(list(month = c("January", "January", "January", "January",
"January", "February", "February", "February", "February", "February",
"March", "March", "March", "March", "March", "April", "April",
"April", "April", "April", "May", "May", "May", "May", "May",
"June", "June", "June", "June", "June", "July", "July", "July",
"July", "July", "August", "August", "August", "August", "August",
"September", "September", "September", "September", "September",
"October", "October", "October", "October", "October", "November",
"November", "November", "November", "November", "December", "December",
"December", "December", "December", "January", "January", "January",
"January", "January", "February", "February", "February", "February",
"February", "March", "March", "March", "March", "March", "April",
"April", "April", "April", "April", "May", "May", "May", "May",
"May", "June", "June", "June", "June", "June", "July", "July",
"July", "July", "July", "August", "August", "August", "August",
"August", "September", "September", "September", "September",
"September", "October", "October", "October", "October", "October",
"November", "November", "November", "November", "November", "December",
"December", "December", "December", "December"), n_inf = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 1L, 1L, 1L, 1L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2L, 2L, 2L, 2L, 2L,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 3L, 3L, 3L, 3L, 3L, 15L, 15L, 15L, 15L, 15L,
38L, 38L, 38L, 38L, 38L, 10L, 10L, 10L, 10L, 10L, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), zone = c("Zone 3",
"Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3",
"Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3",
"Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3",
"Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3",
"Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3",
"Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3",
"Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3",
"Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3", "Zone 3",
"Zone 3", "Zone 3", "Zone 3", "Zone 4", "Zone 4", "Zone 4", "Zone 4",
"Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4",
"Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4",
"Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4",
"Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4",
"Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4",
"Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4",
"Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4",
"Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4", "Zone 4"
), fac1 = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 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, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L),
variables = c("pct0", "pct1", "pct2", "pct3", "pct4", "pct0",
"pct1", "pct2", "pct3", "pct4", "pct0", "pct1", "pct2", "pct3",
"pct4", "pct0", "pct1", "pct2", "pct3", "pct4", "pct0", "pct1",
"pct2", "pct3", "pct4", "pct0", "pct1", "pct2", "pct3", "pct4",
"pct0", "pct1", "pct2", "pct3", "pct4", "pct0", "pct1", "pct2",
"pct3", "pct4", "pct0", "pct1", "pct2", "pct3", "pct4", "pct0",
"pct1", "pct2", "pct3", "pct4", "pct0", "pct1", "pct2", "pct3",
"pct4", "pct0", "pct1", "pct2", "pct3", "pct4", "pct0", "pct1",
"pct2", "pct3", "pct4", "pct0", "pct1", "pct2", "pct3", "pct4",
"pct0", "pct1", "pct2", "pct3", "pct4", "pct0", "pct1", "pct2",
"pct3", "pct4", "pct0", "pct1", "pct2", "pct3", "pct4", "pct0",
"pct1", "pct2", "pct3", "pct4", "pct0", "pct1", "pct2", "pct3",
"pct4", "pct0", "pct1", "pct2", "pct3", "pct4", "pct0", "pct1",
"pct2", "pct3", "pct4", "pct0", "pct1", "pct2", "pct3", "pct4",
"pct0", "pct1", "pct2", "pct3", "pct4", "pct0", "pct1", "pct2",
"pct3", "pct4"), vals = c(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0.999,
0.001, 0, 0, 0, 0.999, 0.001, 0, 0, 0, 0.895, 0.094, 0.011,
0, 0, 0.638, 0.318, 0.043, 0, 0, 0.323, 0.492, 0.185, 0,
0, 0.369, 0.489, 0.142, 0, 0, 0.678, 0.254, 0.067, 0.002,
0, 0.962, 0.036, 0.002, 0, 0, 0.994, 0.005, 0.001, 0, 0,
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0.99, 0.008,
0.002, 0, 0, 0.975, 0.024, 0.001, 0, 0, 0.77, 0.17, 0.061,
0, 0, 0.356, 0.442, 0.201, 0, 0, 0.137, 0.513, 0.349, 0,
0, 0.194, 0.481, 0.325, 0.001, 0, 0.495, 0.307, 0.192, 0.006,
0, 0.889, 0.1, 0.01, 0.001, 0, 0.986, 0.013, 0.001, 0, 0,
1, 0, 0, 0, 0)), row.names = c(NA, -120L), class = "data.frame")
And here is my code:
library(tidyverse)

g1 <- ggplot(data = df2, aes(x=month)) +
geom_bar(aes(y=vals, fill=factor(variables, labels=label_map)),
stat = "identity", show.legend = F) +
scale_fill_manual(values=blues1, name = "Score") +
geom_segment(aes(x = gr_mo1, xend = gr_mo1, y = 0,
yend = 1), linetype = "dashed", color = "gray", linewidth = 2) +
geom_segment(aes(x = gr_mo2, xend = gr_mo2, y = 0,
yend = 1), linetype = "dashed", color = "black", linewidth = 2) +
geom_point(aes(y = n_inf/fac1), col = "brown", size = 3) +
labs(x = "Month", y = "Percentage Cells Covered by Score") + theme(panel.grid.major.x= element_blank(), panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank(), axis.title.x = element_blank(),
plot.title = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 12, face = "bold", color = "black"),
axis.text.y.left = element_text(size = 14, face = "bold", color = "black"),
axis.text.y.right = element_text(size = 14, face = "bold", color = "black")) +
scale_x_discrete(labels = newlabs)
#scale_y_continuous(sec.axis = sec_axis(~.*df1$fac1, name = "No. Infections"))

g1 + facet_wrap(vars(factor(zone, levels = c("Zone 3", "Zone 4"))),
labeller = labeller(zone = c("Zone 3", "Zone 4")))

add

ggtext::geom_textbox(
    aes(x = gr_mo1, y = 1,
        label = "Box here with some kind of text"), 
    color = "red", size = 2,
    box.color = "darkred",
    box.r = unit(0.001, "npc"),
    width = 0.08, height = 0.05,
           hjust = 0)
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.