简体   繁体   English

使用 ggplot 和 ggmap 的 plot 光栅图像中的问题

[英]Problem in plot raster image using ggplot and ggmap

I would like to realize a plot of raster binary map using ggplot2 and also I would like to add as background a stamen map using ggmap. I would like to realize a plot of raster binary map using ggplot2 and also I would like to add as background a stamen map using ggmap. Following different posts I realize these code lines: '在不同的帖子之后,我意识到这些代码行:'

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

I obtain this error message我收到此错误消息

“Error: geom_raster only works with Cartesian coordinates Run rlang::last_error() to see where the error occurred. “错误:geom_raster 仅适用于笛卡尔坐标运行rlang::last_error()以查看错误发生的位置。 In addition: Warning message: Removed 169728 rows containing missing values (geom_raster)""另外:警告消息:删除了包含缺失值的 169728 行 (geom_raster)""

Here the link for a small sample of image https://drive.google.com/drive/u/0/folders/1945TBCzW9lmKjOaN_4CwoD_XfT9PXoO5这里是图像https://drive.google.com/drive/u/0/folders/1945TBCzW9lmKjOaN_4CwoD_XfT9PXoO5的小样本的链接

I'm not very familiar with this package, I have always used the classic drawing functions.这个package我不是很熟悉,一直用经典的绘图功能。 Could anyone help me??谁能帮帮我?? thanks in advance提前致谢

Try adding coord_cartesian() to finalmap :尝试将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)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM