I'm trying to put an image on a time series graph using annotation_raster but because the y value is a date I keep getting the error"
"Error in UseMethod("rescale") : no applicable method for 'rescale' applied to an object of class "character"
Is there a way to do this using annotation_raster or is another method better?
require(ggplot2)
require(ggpubr)
library(png)
###Set working directory
wd <- "E:/R/DataViz/Data" # top level working directory
setwd(wd)
###Make Data Frame
Dates <- seq(as.Date('2020-01-01'), as.Date('2020-01-31'), by = 'days')
Number <- seq(1:31)
DF <- data.frame(Dates,Number)
###Import PNG
mypngfile <- download.file('http://api.altmetric.com/donut/502878_64x64.png', destfile = 'mypng.png', mode = 'wb')
PNG <- readPNG("mypng.png")
####Plot Data
ggplot(DF,aes(x = Dates, y = Number))
geom_line()
annotation_raster(PNG, ymin = 20,ymax = 30,
xmin =
"2020-01-06",
xmax =
"2020-01-27")
CodePudding user response:
Since your x
values are date values, you need to use Dates for your xmin/xmin
in the call to annotation_raster
. Use
ggplot(DF,aes(x = Dates, y = Number))
geom_line()
annotation_raster(PNG, ymin = 20,ymax = 30,
xmin =
as.Date("2020-01-06"),
xmax =
as.Date("2020-01-27"))