Adding a square bracket below the x axis on ggplot

60 Views Asked by At

I have below image created based on a dataset using ggplot. X variable is categorical having values as x= c(w33, w34, w35,.....). Y variable is continuous which goes from 0 to maximum of 60. It has two line graphs as you can see in the below picture.

Current plot : what I have right now

Here's the code to get the above plot.

Graph_6A <- ggplot(data=Data_final, aes(x=EpiWeek, group=1)) + 
  expand_limits(y=c(0,80)) +
  labs(x="Epi Week", y="Count") +
  geom_line(aes(y = Y2022 ), color='darkblue', size = 0.5) +
  geom_line(aes(y = Y2023 ), color='red', size = 0.5) +
  geom_point(aes(x=Data_final$EpiWeek,y=Data_final$Y2022), color="darkblue", size=1, shape = 16, fill="darkblue") +
  geom_point(aes(x=Data_final$EpiWeek,y=Data_final$Y2023), color="red", size=1, shape = 16, fill="red") +
  scale_x_discrete(breaks = levels(Data_final$EpiWeek)[floor(seq(1, nlevels(Data_final$EpiWeek), 
                                                                 length.out = 12))]) +
  theme(legend.position = "top", legend.direction ="horizontal", panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), panel.background = element_rect(fill = 'white'), legend.key.size = unit(0.3, 'cm'),
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, size = 8),
        axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "gray"), 
        legend.key =element_blank()) +
  scale_y_continuous(limits = c(0, 80), breaks = seq(0, 80, by = 20))+
  annotate("text", x = "w33", y = -1, label = "Oct 2022")

I want to add a square bracket below the x axis as it goes from x= w33 to x= 46. It should be labeled as October 2022. Please refer to the below image created using word.

Desired output: what I want

I tried using the geom_bracket function in R. But it doesn't allow me to draw the bracket below the x axis as seen on the picture above.

Here's the code I've been trying.

Graph_6A + geom_bracket(
    xmin = "w33", xmax = "w46", y.position = -1,
    label = "october"
  )

You kind help is highly appreciated.

0

There are 0 best solutions below