在获取背景图像时向背景图像添加半透明层时出现错误:输入无效:date_trans仅适用于Date类的对象 [英] While adding a semi transparent layer to background image in r getting Error: Invalid input: date_trans works with objects of class Date only
问题描述
我正在尝试在r图的背景图像中添加半透明的黑色叠加层.它使用 annotate
进行工作,并从以下解决方案中获取解决方案:
如果我在xscale上将其从 -Inf扩展为Inf : annotate("rect",xmin = min(ts_all_long $ date),xmax = max(ts_all_long $ date),ymin = -Inf,ymax = Inf,填充=黑色",alpha = 0.3)+
然后给出错误错误:输入无效:date_trans仅适用于Date类的对象
问题是 scales :: date_trans()
转换不能正常处理数字输入.我通过手动构造无限日期为该问题找到了(v1.0.0)创建于2021-04-20 sup>
我已经争论过,在此之前,最好是日期和时间scales软件包中的转换应该更灵活一些,以处理此类情况.
I am trying to add a semitransparent overlay black layer to background image in r plot. It worked by using annotate
and got the solution from: How to add a black overlay semi transparent layer above the background image in r?
Issue: Semi transparent layer doesn't extend completely from left to right over the image of the plot.
If I try use xmin = -Inf, xmax = Inf
then it gives error due to date scale xaxis.
So how do i cover the whole image with the layer ?
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))
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)
If I extend this from -Inf to Inf on xscale: annotate("rect", xmin = min(ts_all_long$date), xmax = max(ts_all_long$date), ymin = -Inf, ymax = Inf, fill = "black", alpha = 0.3) +
then it gives error Error: Invalid input: date_trans works with objects of class Date only
The problem is that the scales::date_trans()
transformation doesn't gracefully handle numeric input. I've found a workaround for this issue by manually constructing infinite dates. Example with a standard dataset below:
library(ggplot2)
ggplot(economics, aes(date, unemploy)) +
geom_line() +
annotate("rect", xmin = -Inf, xmax = Inf,
ymin = -Inf, ymax = Inf, fill = "black", alpha = 0.5)
#> Error: Invalid input: date_trans works with objects of class Date only
# Manually constructing infinite dates
ggplot(economics, aes(date, unemploy)) +
geom_line() +
annotate("rect",
xmin = structure(-Inf, class = "Date"),
xmax = structure(Inf, class = "Date"),
ymin = -Inf, ymax = Inf, fill = "black", alpha = 0.5)
Created on 2021-04-20 by the reprex package (v1.0.0)
I've argued before that, ideally, the date and time transformations in the scales package should be a little bit more flexible to handle these kind of cases.
这篇关于在获取背景图像时向背景图像添加半透明层时出现错误:输入无效:date_trans仅适用于Date类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!