在R中组合地图和XY GGR绘图图

在R中组合地图和XY GGR绘图图,r,ggplot2,ggpubr,R,Ggplot2,Ggpubr,我需要将一张代表考古遗址的地图与不同考古对象的平面图结合起来。地图位于tiff文件中,必须遵守其比例 首先,这是地图,以红色突出显示参考比例(例如,在X轴上,从-6000到-4000有20米的距离;在Y轴上,从900到2100有12米)。 我的ggplot图表是通过运行以下代码获得的: archaeo <- ggplot() + geom_ellipsis(data=Unit_H, aes(x0 = X, y0 = Y, a = Diameter_E.W/2+

我需要将一张代表考古遗址的地图与不同考古对象的平面图结合起来。地图位于tiff文件中,必须遵守其比例

首先,这是地图,以红色突出显示参考比例(例如,在X轴上,从-6000到-4000有20米的距离;在Y轴上,从900到2100有12米)。

我的ggplot图表是通过运行以下代码获得的:

archaeo <- ggplot() + 
    geom_ellipsis(data=Unit_H, 
        aes(x0 = X, y0 = Y, a = Diameter_E.W/2+250, b = Diameter_N.S/2+250, angle = 0), 
        lwd=0, col="darkgray", fill="gray", alpha=0.15) +     
    geom_ellipsis(data=Unit_H, 
        aes(x0 = X, y0 = Y, a = Diameter_E.W/2+120, b = Diameter_N.S/2+120, angle = 0), 
        lwd=0, col="darkgray", fill="gray", alpha=0.25) + 
    geom_ellipsis(data=Unit_H, 
        aes(x0 = X, y0 = Y, a = Diameter_E.W/2, b = Diameter_N.S/2, angle = 0), 
        lwd=0.5, col="darkgray", fill="gray", alpha=0.75) + 
    geom_point(data=Unit_H, aes(X, Y), size = 0.5) + 
    geom_point(data=Refits_H_trans, aes(x,y,group=sample, colour=factor(sample))) + 
    geom_line(data=Refits_H_trans, lwd=0.2, lty=1, aes(x,y, group=sample, colour=factor(sample))) + 
    coord_fixed() + 
    theme_bw() + 
    theme(legend.position="none") + 
    ggtitle("Unit H") + 
    xlim(-6600,-3800) + 
    ylim(400,2400)

archeo我重新创建了一个存在此问题的简单示例:

library(tidyverse)

# create the background
bck_square <- data.frame(x=c(1,1,0,0),y=c(0,1,1,0))
p <- ggplot(bck_square, aes(x=x, y=y)) +
  geom_point(size=10, color="red") + 
  theme(panel.border=element_blank(), 
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        #panel.background=element_blank(), keep the background to see where image ends
        axis.text=element_blank(),
        axis.ticks=element_blank(),
        axis.title=element_blank())
p

但是,通过使用
annotation\u custom
(请参见指南),您可以调整图像的最小值和最大值的位置。通过使用边界参数,我可以得到图像背景和人物

library(png)
library(grid)
min_border <- .064
max_border <- .061
ggplot(bck_square, aes(x, y)) +
  annotation_custom(g,xmin=-min_border, xmax=1+max_border, ymin=-min_border, ymax=1+max_border) +
  geom_point()
库(png)
图书馆(网格)

min_border我不太清楚,无法给出完整的答案,但我相信你可以将图像转换为光栅,并为其指定地理信息——坐标、投影等。此外,还有一些关于这方面的问题,我不确定我是否理解预期的输出。仅使用smthg(如
theme\u void()
删除ggplot的轴是不够的?谢谢您的评论。我试试看@DJack,我需要的是用ggplot的X和Y值对png图像进行地理参考,因此移除轴不能保证图片具有良好的地理参考。还有什么想法吗?要做到这一点,你需要知道png和ggplot的投影(
crs
)。
ggsave("temp.png",p)
img <- readPNG("temp.png")
library(ggpubr)
ggplot(bck_square, aes(x, y)) +
  background_image(img) +
  geom_point()
library(png)
library(grid)
min_border <- .064
max_border <- .061
ggplot(bck_square, aes(x, y)) +
  annotation_custom(g,xmin=-min_border, xmax=1+max_border, ymin=-min_border, ymax=1+max_border) +
  geom_point()