如何在 r 中的背景图像上方添加黑色覆盖半透明层?
How to add a black overlay semi transparent layer above the background image in r?
我在绘图中使用 背景图像 但绘图的颜色与图像混合,所以我想添加一个 覆盖半透明黑色将图层添加到背景图像 以便绘图颜色可以突出并且不与背景混合..
我试过添加 geom_rect()
但效果不佳 geom_rect(aes(xmin = min(date), xmax=max(date), ymin = -Inf, ymax=Inf), fill = "black", alpha = 0.3)
我怎样才能在整个背景图像上添加一个半透明的覆盖层。
结果失败
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))
没有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)
与 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)
基于 this answer 我尝试了 annotate()
而不是 geom_rect()
,这就成功了:
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)
由 reprex package (v2.0.0)
于 2021-04-20 创建
不过,我认为这样做实际上会更好看:
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)
由 reprex package (v2.0.0)
于 2021-04-20 创建
我在绘图中使用 背景图像 但绘图的颜色与图像混合,所以我想添加一个 覆盖半透明黑色将图层添加到背景图像 以便绘图颜色可以突出并且不与背景混合..
我试过添加 geom_rect()
但效果不佳 geom_rect(aes(xmin = min(date), xmax=max(date), ymin = -Inf, ymax=Inf), fill = "black", alpha = 0.3)
我怎样才能在整个背景图像上添加一个半透明的覆盖层。
结果失败
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))
没有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)
与 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)
基于 this answer 我尝试了 annotate()
而不是 geom_rect()
,这就成功了:
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)
由 reprex package (v2.0.0)
于 2021-04-20 创建不过,我认为这样做实际上会更好看:
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)
由 reprex package (v2.0.0)
于 2021-04-20 创建