【发布时间】:2022-06-18 01:54:19
【问题描述】:
当我运行以下代码时(来源:embed image: ggplot to plotly date issue):
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)
我有这个错误信息:
Error in if (RasterGeom/length(geoms) > 0.5) "above" else "below" :
the condition has length > 1
(我无法对答案发表评论,所以我提出了一个新问题)
非常感谢
【问题讨论】:
-
我看到了这些消息,但它们是对我的警告。您是否准确复制了消息?对您来说是错误还是警告?
-
对我来说是个错误
标签: r ggplot2 plotly png raster