我在绘图中使用背景图像,但绘图的颜色与图像混合,所以我想在背景图像中添加一个覆盖半透明黑色层 以便绘图颜色可以突出并且不与背景混合..
我曾尝试添加 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)
答案 0 :(得分:0)
基于 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 年 4 月 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 年 4 月 20 日创建