0

I am using background image in the plot but the colors of the plot is mixing up with the image so I would like to add an overlay semitransparent black layer to the background image so that plot colors can stand out and do not mix with the background..

enter image description here

I have tried adding geom_rect() but that doesn't work well geom_rect(aes(xmin = min(date), xmax=max(date), ymin = -Inf, ymax=Inf), fill = "black", alpha = 0.3)

How can I just add a semitransparent overlay layer over the complete background image.

failed result

enter image description here

df

library(tidyverse)
library(lubridate)
library(ggpubr)
library(grid)
library(jpeg)

file_url1 <- "https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/ts_all_long4.csv"

ts_all_long <- read.csv(url(file_url1))

ts_all_long <- ts_all_long %>%
  mutate(date = as.Date(date))

image used: https://github.com/johnsnow09/covid19-df_stack-code/blob/a00c8820363f2163837d24f801f7c5b85167e0aa/coronavirus-4972480_1920.jpg

without geom_rect():

ts_all_long %>% 
  filter(Country.Region == "Brazil") %>% 
  
  ggplot(aes(x = date, y = Confirmed_daily)) +
  background_image(readJPEG("Covid 19 images/coronavirus-4972480_1920.jpg")) +
  geom_area(size = 1, col = "#f08080", fill = "#f08080", alpha = 0.5)

with geom_rect():

ts_all_long %>% 
  filter(Country.Region == "Brazil") %>% 
  
  ggplot(aes(x = date, y = Confirmed_daily)) +
  background_image(readJPEG("Covid 19 images/coronavirus-4972480_1920.jpg")) +
  geom_rect(aes(xmin = min(date), xmax=max(date), ymin = -Inf, ymax=Inf),
            fill = "black", alpha = 0.3) +
  geom_area(size = 1, col = "#f08080", fill = "#f08080", alpha = 0.5)
ViSa
  • 1,563
  • 8
  • 30

1 Answers1

0

Based on this answer I tried annotate() instead of geom_rect(), which does the trick:

ts_all_long %>% 
  filter(Country.Region == "Brazil") %>% 
  
  ggplot(aes(x = date, y = Confirmed_daily)) +
  background_image(readJPEG("/home/johannes/Downloads/coronavirus-4972480_1920.jpg")) +
  annotate("rect", xmin = min(ts_all_long$date), xmax = max(ts_all_long$date), ymin = -Inf, ymax = Inf,
            fill = "black", alpha = 0.3) +
  geom_area(size = 1, col = "#f08080", fill = "#f08080", alpha = 0.5)

Created on 2021-04-20 by the reprex package (v2.0.0)

However, I think it would actually look better to do something like this:

ts_all_long %>% 
  filter(Country.Region == "Brazil") %>% 
  
  ggplot(aes(x = date, y = Confirmed_daily)) +
  background_image(readJPEG("/home/johannes/Downloads/coronavirus-4972480_1920.jpg")) +
  geom_area(size = 1, col = "white", fill = "#f08080", alpha = 0.5)

Created on 2021-04-20 by the reprex package (v2.0.0)

JBGruber
  • 11,727
  • 1
  • 23
  • 45
  • Thanks @JBGruber, really appreciate your help. I was clueless that why `geom_rect()` was not working & was trying `geom_segment()` and never thought could do that with `annotate`. Just one more thing any idea how can I extend this layer from `-Inf` to `Inf` on x axis date scale as the whole background should look same . I tried `annotate("rect", xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, fill = "black", alpha = 0.3)` but getting an error: `Error: Invalid input: date_trans works with objects of class Date only` – ViSa Apr 20 '21 at 10:30
  • Have also raised this in another SO Post https://stackoverflow.com/questions/67177555/while-adding-a-semi-transparent-layer-to-background-image-in-r-getting-error-in – ViSa Apr 20 '21 at 10:59