Shading a particular area on a line graph

Hi,
I have a data of growth rate year-wise. I have 2 issues here:

  1. An additional requirement is that an economic crisis happened in 1991. I need to shade that area with a particular color (let's say grey) to highlight that a crisis was there.
  2. I need to give Y-axis scale from 0 to 14. Now it is showing 6 to 14.

How can I solve these issues?

library(tidyverse)

data<-tibble::tribble(
  ~year, ~growth,
  1985L,     12L,
  1986L,     12L,
  1987L,     12L,
  1988L,      9L,
  1989L,     14L,
  1990L,      6L,
  1991L,     13L,
  1992L,     11L,
  1993L,      6L,
  1994L,     14L,
  1995L,      6L,
  1996L,     13L,
  1997L,      8L,
  1998L,     12L,
  1999L,      6L,
  2000L,      9L
  )

data %>% 
  ggplot(aes(year,growth))+
  geom_line(size=1.5)+
  theme_minimal()

to draw the shaded area you can use "annotate" and draw a rectangle, defined by its 4 corners.

data %>% 
  ggplot(aes(year,growth))+

  theme_minimal() +
  # show a rect defining the time
  annotate(geom = "rect",
           xmin = 1990.5, xmax = 1991.5,
           ymin = -Inf, ymax = Inf,
           fill = "grey", alpha = 0.2) +
  # add some text
  annotate(geom = "text",
           x = 1992, y = 15,
           label = "The crisis started in 1991",
           fontface = "italic", hjust = 0) +
  geom_line(size=1.5)+ 
  scale_y_continuous(limits = c(0, 15))   # set the limits to include the 0

1 Like

Thanks a lot. But just an add-on, what does ymin= Inf mean?

Inf and -Inf refers to the boundaries of the plot, in this way you don't have to tweak the numbers so it matches what is shown but just draws it along the full y-axis range.

thank you very much.

Regards,
Nithin

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.