gpt4 book ai didi

r - 嵌入图像 : ggplot to plotly date issue

转载 作者:行者123 更新时间:2023-12-04 03:33:33 25 4
gpt4 key购买 nike

我正在尝试使用 ggplotly 将以下 ggplot 图像转换为 plotly:

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

enter image description here

我不能使用 ggplotly 直接转换它,因为 geom_image 没有在 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

我认为您需要以不同的方式处理它,删除 geom_image 并在 layout 中添加图像。我不知道如何引用图像应该经过的位置(即 df$image)。

即使手动完成,我也无法在图表上获得一张图片:

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
)))

enter image description here

solution建议我需要使用不同的日期格式,但我也无法使用它:

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

是否有任何灵活的建议可以让我引用我想要图像的所有点?

谢谢

最佳答案

我不确定你的问题是日期问题,因为我无法使用 layout.image 在绘图上获取图像,即使我使用 1:7 而不是日期,但话又说回来我不是一个阴谋专家。也就是说,我确信有几种方法可以在不使用 geom_image 的情况下在绘图上获取图像,这是一种不是很干净的方法 - 并且对于大数据帧可能效率低下 - 但它有效有 plotly :

library(ggplot2)
library(png)
library(RCurl)
library(plotly)
mydf <- 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))
mydf

# You should find some way to compute your desired image height and image width
yHeight <- (max(mydf$counts) - min(mydf$counts)) * 0.05
xWidth <- (max(as.numeric(mydf$date)) - min(as.numeric(mydf$date))) * 0.05

# create the base plot
gg2 <- ggplot(mydf, aes(date, counts)) +
geom_line()

# for each row in the df
for (x in 1:nrow(mydf)) {
row <- mydf[x, ]
if(!is.na(row$image)){
# read the image
img <- readPNG(getURLContent(row$image))
# add the image with annotation_raster
gg2 <- gg2 + annotation_raster(img,
xmin = as.numeric(row$date) - xWidth/2,
xmax = as.numeric(row$date) + xWidth/2,
ymin = row$counts - yHeight/2,
ymax = row$counts + yHeight/2)
}
}

ggplotly(gg2)

enter image description here

关于r - 嵌入图像 : ggplot to plotly date issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67383385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com