如何在R中的背景图像上方添加黑色覆盖半透明层? [英] How to add a black overlay semi transparent layer above the background image in r?

查看:79
本文介绍了如何在R中的背景图像上方添加黑色覆盖半透明层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在绘图中使用了背景图像,但是绘图中的颜色与图像混合在一起,所以我想在背景图像中添加一个覆盖半透明黑色层strong>,以使绘图颜色能够突出显示并且不会与背景混合.

我尝试添加 geom_rect(),但效果不佳 geom_rect(aes(xmin = min(date),xmax = max(date),ymin = -Inf,ymax = Inf),填充=黑色",alpha = 0.3)

如何在整个背景图像上添加一个半透明的覆盖层.

失败的结果

df

 库(tidyverse)库(润滑)图书馆(ggpubr)图书馆(网格)图书馆(jpeg)file_url1<-" https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/ts_all_long4.csvts_all_long<-read.csv(url(file_url1))ts_all_long<-ts_all_long%>%mutate(日期= as.Date(日期)) 

使用的图像:(v2.0.0)创建于2021-04-20 sup>

但是,我认为这样做实际上看起来会更好:

  ts_all_long%>%filter(Country.Region ==巴西")%&%ggplot(aes(x =日期,y =确认每日))+background_image(readJPEG("/home/johannes/Downloads/coronavirus-4972480_1920.jpg"))+geom_area(大小= 1,col =白色",填充=#f08080",alpha = 0.5) 

reprex软件包(v2.0.0)创建于2021-04-20 sup>

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..

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

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)

解决方案

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)

这篇关于如何在R中的背景图像上方添加黑色覆盖半透明层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆