I am trying to convert the following ggplot
image to plotly
using ggplotly
:
library(tidyverse)
library(ggimage)
library(plotly)
df <- data.frame(date =
as.Date(c("01/01/1998", "10/01/1998", "15/01/1998",
"25/01/1998", "01/02/1998", "12/02/1998", "20/02/1998"), "%d/%m/%Y"),
counts = c(12, 10, 2, 24, 15, 1, 14),
image = c(NA, "https://www.r-project.org/logo/Rlogo.png", NA, NA,
"https://www.r-project.org/logo/Rlogo.png", NA, NA))
df
# date counts image
# 1 1998-01-01 12 <NA>
# 2 1998-01-10 10 https://www.r-project.org/logo/Rlogo.png
# 3 1998-01-15 2 <NA>
# 4 1998-01-25 24 <NA>
# 5 1998-02-01 15 https://www.r-project.org/logo/Rlogo.png
# 6 1998-02-12 1 <NA>
# 7 1998-02-20 14 <NA>
gg <- ggplot(df, aes(date, counts)) +
geom_line() +
geom_image(aes(image = image), size = 0.05)
gg
I cant directly convert it using ggplotly because geom_image
is not implemented in plotly
:
ggplotly(gg, height = 700, width = 900)
# In geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
# geom_GeomImage() has yet to be implemented in plotly.
# If you'd like to see this geom implemented,
# Please open an issue with your example code at
# https://github.com/ropensci/plotly/issues
I think you need to go about it a different way by removing geom_image
and adding in an image in layout
. I don't know how to reference the locations of where the image should be though through (i.e. df$image
).
Even doing it manually I can't get one image on the graph:
gg2 <- ggplot(df, aes(date, counts)) +
geom_line()
ggplotly(gg2, height = 700, width = 900) %>%
layout(
images = list(
list(source = "https://www.r-project.org/logo/Rlogo.png",
xref = "paper", #https://plotly.com/r/reference/
yref = "paper",
x = .2, #as.Date(c("10/01/1998"), "%d/%m/%Y"),
y = .2,
sizex = 0.1,
sizey = 0.1,
opacity = 0.8
)))
This solution suggests I need to work in a different date format but I can't get it to work either:
ggplotly(gg2, height = 700, width = 900) %>%
layout(
images = list(
list(source = "https://www.r-project.org/logo/Rlogo.png",
xref = "x", #or paper
yref = "y", #or paper
x = as.numeric(as.POSIXct("10/01/1998", format = "%d/%m/%Y")), #or .3
y = 10, #or .4
sizex = 0.1,
sizey = 0.1,
opacity = 0.8
)))
#or, reformat the x axis first
ggplotly(gg2, height = 700, width = 900) %>%
layout(xaxis = list(range =
c(as.numeric(as.POSIXct("01/01/1998", format="%d/%m/%Y"))*1000,
as.numeric(as.POSIXct("20/02/1998", format="%d/%m/%Y"))*1000),
type = "date"),
images = list(
list(source = "https://www.r-project.org/logo/Rlogo.png",
xref = "x",
yref = "y",
x = as.numeric(as.POSIXct("10/01/1998", format="%d/%m/%Y"))*1000,
y = 10,
sizex = 0.1,
sizey = 0.1,
opacity = 0.8
)))
#converting date from the outset using as.POSIXct doesn't help
#df$date <- as.numeric(as.POSIXct(df$date, format="%d/%m/%Y"))*1000
Any flexible suggests that allows me to reference all the points at which I want an image?
thanks