我有一个我希望循环并适合R中的边界框(坐标)的图像列表。
我可以像这样绘制一个简单的图像:
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g <- rasterGrob(img, interpolate=TRUE,width = 1, height=1)
df <- data.frame(x=(1:100),
y=(1:100))
ggplot(df, aes(x,y)) +
annotation_custom(g, xmin=20, xmax=90, ymin=75, ymax=100)
编辑:感谢您的帮助!搞定了:
library(png)
library(grid)
library(ggplot2)
img_path <- "C:/flickr_london_new2/"
setwd(img_path)
df <- data.frame(x=(0:0),
y=(0:0))
plot_files <- lapply(list.files(img_path, pattern="*.png"),
function(x) list(unlist(strsplit(gsub(".png","",x), "_")),x)
)
p <- ggplot(df, aes(x,y))
for (i in plot_files) {
bb <- i[[1]] # Bounding Box
im_loc <- i[[2]] # Image
img <- readPNG(im_loc)
g <- rasterGrob(img, interpolate=TRUE, width = 1, height=1)
p <- p + annotation_custom(g, xmin=as.numeric(unlist(bb)[3]),
xmax=as.numeric(unlist(bb)[4]),
ymin=as.numeric(unlist(bb)[1]),
ymax=as.numeric(unlist(bb)[2]))
}
p <- p + ylim(51.46, 51.55) + xlim(-0.22,-0.05)
#P <- p + coord_equal(ratio=122/78)
# Plot
p