在 r 中向背景图像添加半透明层时出现错误:无效输入: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

查看:29
本文介绍了在 r 中向背景图像添加半透明层时出现错误:无效输入:date_trans 仅适用于类 Date 的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 r 图中向 背景图像 添加 半透明覆盖黑色层.它通过使用 annotate 并从以下位置获得解决方案:

如果我在 xscale 上将其从 -Inf 扩展到 Inf:annotate("rect", xmin = min(ts_all_long$date), xmax = max(ts_all_long$date),ymin = -Inf, ymax = Inf, fill = "black", alpha = 0.3) +

然后它给出错误 错误:无效输入:date_trans 仅适用于 Date 类的对象

解决方案

问题在于 scales::date_trans() 转换不能正常处理数字输入.我通过手动构建无限日期找到了解决此问题的 (v1.0.0) 于 2021 年 4 月 20 日创建

我在此之前争论过,理想情况下,日期和时间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))

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

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.

这篇关于在 r 中向背景图像添加半透明层时出现错误:无效输入:date_trans 仅适用于类 Date 的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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