R从geotiff与ggplot2绘制背景地图 [英] R plot background map from Geotiff with ggplot2

查看:910
本文介绍了R从geotiff与ggplot2绘制背景地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用R base图,我可以使用以下命令绘制任何geotiff:

  library(raster)
plot(raster(geo.tiff))

例如,下载



颜色:




With the R base plot, I can plot any geotiff with the following command:

library("raster")
plot(raster("geo.tiff"))

For example, downloading this data, I would do the follwing:

setwd("C:/download") # same folder as the ZIP-File
map <- raster("smr25musterdaten/SMR_25/SMR_25KOMB_508dpi_LZW/SMR25_LV03_KOMB_Mosaic.tif")

How do you Plot GeoTif Files in ggplot2?

EDIT:

1: I've replaced the greyscale map from the sample files with a coloured map to ilustrate the problem of the missing colortable.

2: With the help of Pascals answer, I was able to adapt and improve this solution and make it more dynamic to the input tif. I will post the answer below.

解决方案

Here is an alternative using function gplot from rasterVis package.

library(rasterVis)
library(ggplot2)
setwd("C:/download") # same folder as the ZIP-File
map <- raster("smr25musterdaten/SMR_25/SMR_25KGRS_508dpi_LZW/SMR25_LV03_KGRS_Mosaic.tif")

gplot(map, maxpixels = 5e5) + 
  geom_tile(aes(fill = value)) +
  facet_wrap(~ variable) +
  scale_fill_gradient(low = 'white', high = 'black') +
  coord_equal()

If you want to use the color table:

coltab <- colortable(map)
coltab <- coltab[(unique(map))+1]

gplot(map, maxpixels=5e5) + 
  geom_tile(aes(fill = value)) +
  facet_wrap(~ variable) +
  scale_fill_gradientn(colours=coltab, guide=FALSE) +
  coord_equal()

With colors:

这篇关于R从geotiff与ggplot2绘制背景地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆