Line plot with highlighted areas

I am still working with ggplot, and I would like to know if it is possible to achieve some effects. With this dataframe:

visits <- data.frame(
  stringsAsFactors = FALSE,
               Mth = c("01-2019","02-2019","03-2019",
                       "04-2019","05-2019","06-2019","07-2019","08-2019",
                       "09-2019","10-2019","11-2019","12-2019","01-2020",
                       "07-2020","08-2020","09-2020","10-2020","04-2021",
                       "05-2021","06-2021","07-2021"),
               Nat = c(3705L,4983L,5139L,6123L,
                       6407L,7426L,9030L,12579L,7332L,7990L,5772L,5216L,
                       3943L,5007L,8816L,5540L,4831L,4693L,4317L,3977L,
                       10910L),
               Ext = c(238L,160L,601L,1915L,3823L,
                       3241L,2277L,2487L,3803L,3888L,526L,364L,413L,
                       225L,531L,254L,334L,60L,393L,421L,2097L)

And this code:

ggplot(data = visits) +
  aes(x=Mth, group=1) + 
  scale_x_discrete (limits = c("04-2019","05-2019","06-2019","07-2019","08-2019","09-2019","10-2019","11-2019",
                               "12-2019","01-2020","07-2020","08-2020","09-2020","10-2020","04-2021", "05-2021", "06-2021", "07-2021")) +
  scale_y_continuous (limits = c(0000, 14000), breaks = seq(0000, 14000, by = 2000), 
                     labels = function(x) format(x, big.mark = ".", decimal.mark = ",")) +
  geom_line(aes(y = Nat), color = "darkred") + 
  geom_line(aes(y = Ext), color="steelblue", linetype="twodash") +
  labs(x="", y="Visits",
       title="Visits evolution",
       subtitle="From april 2019 to july 2021",
       caption="") +
  theme_ipsum()

I get this:

I would like to get this (it is a recreation with photoshop, I have not been able to do it with R).


is it possible? Also, does anyone know why I don't see the legend on the graph?
Thanks for your help

Here is a rough version of how I would do that. ggplot works best with data in a long format, so I reshaped the data using the pivot-Longer function from the tidyr package.

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.5

visits <- data.frame(
  stringsAsFactors = FALSE,
  Mth = c("01-2019","02-2019","03-2019",
          "04-2019","05-2019","06-2019","07-2019","08-2019",
          "09-2019","10-2019","11-2019","12-2019","01-2020",
          "07-2020","08-2020","09-2020","10-2020","04-2021",
          "05-2021","06-2021","07-2021"),
  Nat = c(3705L,4983L,5139L,6123L,
          6407L,7426L,9030L,12579L,7332L,7990L,5772L,5216L,
          3943L,5007L,8816L,5540L,4831L,4693L,4317L,3977L,
          10910L),
  Ext = c(238L,160L,601L,1915L,3823L,
          3241L,2277L,2487L,3803L,3888L,526L,364L,413L,
          225L,531L,254L,334L,60L,393L,421L,2097L))
Rects = data.frame(xmin = c("08-2019", "09-2020"), xmax = c("11-2019", "05-2021"), 
                  ymin = c(0,0), ymax = c(14000, 14000))
visits_long <- tidyr::pivot_longer(visits, Nat:Ext, names_to = "Source", values_to = "Value")
  
ggplot(data = visits_long) +
    scale_x_discrete (limits = c("04-2019","05-2019","06-2019","07-2019","08-2019","09-2019","10-2019","11-2019",
                                 "12-2019","01-2020","07-2020","08-2020","09-2020","10-2020","04-2021", "05-2021", "06-2021", "07-2021")) +
    scale_y_continuous (limits = c(0000, 14000), breaks = seq(0000, 14000, by = 2000), 
                        labels = function(x) format(x, big.mark = ".", decimal.mark = ",")) +
    geom_line(aes(x=Mth, y = Value, group=Source, color = Source, linetype = Source)) + 
    geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), 
              data = Rects, fill = "grey50", alpha = 0.3) +
    scale_color_manual(values = c(Nat = "darkred", Ext = "steelblue")) +
    scale_linetype_manual(values = c( Ext = "twodash", Nat = "solid"), guide = NULL) +
    labs(x="", y="Visits",
         title="Visits evolution",
         subtitle="From april 2019 to july 2021",
         caption="")
#> Warning: Removed 6 row(s) containing missing values (geom_path).

Created on 2021-08-24 by the reprex package (v0.3.0)

Works fine. Thanks FJCC.

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.