Add line at top of ggplot2 plot, above title

I would like to know how to add a border to my plot on only one side. I have this plot:


p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  theme_minimal() +
  labs(title = "Title",
       subtitle = "Subtitle")


Created on 2019-12-21 by the reprex package (v0.3.0)

I would like to add a horizontal line above the title, like this:

I cannot figure out how to do this. I have tried theme(plot.background = element_rect()), but this adds a border on all sides of the plot.

Please note that I am looking for a general solution to this problem. Using annotations in which the location of the line is expressed in terms of the data values may not work for my purposes.

Thank you

1 Like

I can't see anyway to do this in the ggplot2 world. It is possible to use some \LaTeX in title with the latex2exp package, but it appears to be limited to the plotmath set, which has underline but no overline. I supposed you could try underline with a long blank string and follow with a \n plus your actual title. It might actually be easier to create a css environment for this, however.

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