使用ggplot和ggmap绘制光栅图像的问题

Problem in plot raster image using ggplot and ggmap

我想使用 ggplot2 实现光栅二进制地图的绘图,我还想使用 ggmap 添加雄蕊图作为背景。在不同的帖子之后,我意识到这些代码行: '

library(sp)
library(raster)
library(rasterVis)
library(ggplot2)
library(ggmap)
library(tmaptools)
library(rgdal)
test <- raster('C:/France_accuracy/img_fig/image_france_2009.tif')
test_df <- rasterToPoints(test)
test_df <- data.frame(test_df)
colnames(test_df) <- c("X","Y","Values")
head(test_df)
#Simple plot using ggplot
p1 <- ggplot() + geom_raster(data=france_df, mapping=aes(X, Y, fill= factor(Values)))
#Background and final map
background <- get_stamenmap(bbox = c(-0.7744, 44.2001,-0.5286, 44.3017),
                            maptype='toner-background', zoom = 5)
finalmap <- ggmap(background) +  
            geom_raster(data=france_df, mapping=aes(X, Y, fill= factor(Values)))

我收到此错误消息

“Error: geom_raster only works with Cartesian coordinates Run rlang::last_error() to see where the error occurred. In addition: Warning message: Removed 169728 rows containing missing values (geom_raster)""

此处 link 为小样本图像 https://drive.google.com/drive/u/0/folders/1945TBCzW9lmKjOaN_4CwoD_XfT9PXoO5

我对这个包不是很熟悉,我一直用的是经典的绘图功能。谁能帮帮我?? 提前致谢

尝试将 coord_cartesian() 添加到 finalmap:

rngX <- range(test_df$X)
rngY <- range(test_df$Y)
background <- get_stamenmap(bbox = c(rngX[1], rngY[1], rngX[2], rngY[2]),    
                            maptype='toner-background', zoom=13)
finalmap <- ggmap(background) + 
            geom_raster(data=test_df, mapping=aes(X, Y, fill= factor(Values)), alpha=0.3) + 
            coord_cartesian() 
print(finalmap)