Home > Blockchain >  Fill the area above and below the central line in geom_step() with different colors
Fill the area above and below the central line in geom_step() with different colors

Time:11-03

For the following PMI data, I would like to reproduce a plot similar to the figure below:

         date Eurozone.Markit.Composite.PMI France.Markit.Composite.PMI Germany.Markit.Composite.PMI
1   2022-1-31                          52.3                        52.7                         53.8
2   2022-2-28                          55.5                        55.5                         55.6
3   2022-3-31                          54.9                        56.3                         55.1
4   2022-4-30                          55.8                        57.6                         54.3
5   2022-5-31                          54.8                        57.0                         53.7
6   2022-6-30                          52.0                        52.5                         51.3
7   2022-7-31                          49.9                        51.7                         48.1
8   2022-8-31                          48.9                        50.4                         46.9
9   2022-9-30                          48.1                        51.2                         45.7
10 2022-10-31                          47.1                        50.0                         44.1

enter image description here

Figure source: enter image description here

References:

R ggplot2: colouring step plot depending on value

CodePudding user response:

Using geom_rect and a bit of data wrangling you could do:

library(ggplot2)
library(dplyr)

df$date <- as.Date(df$date)

df <- df |> 
  mutate(xmax = lead(date), xmin = date, fill = Eurozone.Markit.Composite.PMI > 50)

ggplot(df, aes(x = date))  
  geom_rect(aes(xmin = xmin, xmax = xmax, ymin = 50, ymax = Eurozone.Markit.Composite.PMI, fill = fill), alpha = .6)  
  geom_step(aes(y = Eurozone.Markit.Composite.PMI))  
  geom_point(data = subset(df, date %in% range(date)), aes(y = Eurozone.Markit.Composite.PMI))  
  geom_hline(yintercept = 50)  
  scale_fill_manual(values = c("red", "lightgreen"))  
  guides(fill = "none")
#> Warning: Removed 1 rows containing missing values (geom_rect).

  • Related