【问题标题】:R plot background map from Geotiff with ggplot2R用ggplot2从Geotiff绘制背景图
【发布时间】:2016-01-26 08:25:56
【问题描述】:

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

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

例如,下载this数据,我会这样做:

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

你如何在 ggplot2 中绘制 GeoTif 文件?

编辑:

1:我已将示例文件中的灰度图替换为彩色图,以说明缺少颜色表的问题。

2:在 Pascals 回答的帮助下,我能够适应和改进 this solution 并使输入 tif 更加动态。我将在下面发布答案。

【问题讨论】:

  • 使用您的示例,我在map@legend@colortable 中获得了一个颜色表(266 个值)。你的操作系统和sessionInfo 是什么?
  • @Pascal:昨天一定很晚了。map@legend@colortable 有效,colortable(map) 也有效。

标签: r plot ggplot2 raster


【解决方案1】:

这是使用rasterVis 包中的函数gplot 的替代方法。

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

如果要使用色表:

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

有颜色:

【讨论】:

  • 感谢您的解决方案!问题是,我有两个问题: 1. 您使用scale_fill_gradient(low = 'white', high = 'black') 手动设置填充。当我有彩色地图时,这将不起作用。问题 2:我真的很想使用 ggplot 而不是 gplot,因为我必须向底图添加其他数据。
  • gplotggplot 的包装,如果您阅读帮助页面...为什么您认为ggplot2 已加载?
  • 好吧,我的错,我很抱歉:-/尽管留下问题 1.. 我正在尝试使用 linked SO question / answer by Florian R. Klein 解决它我将编辑/改写我原来的问题。
  • 对样本数据很有帮助,谢谢!奇怪的是,它不适用于我正在使用的数据,这是我提供的示例数据的旧版本。所以我会把我的答案留在那里给任何需要我稍微长一点的解决方案的人。
【解决方案2】:

就像我在原始问题中指出的那样,我能够使用 Pascals 输入和 this solution 解决问题。这是正确显示颜色的方式:

library(rasterVis) # in order to use raster in ggplot
setwd("C:/download") # same folder as the ZIP-File

map <- raster("smr25musterdaten/SMR_25/SMR_25KOMB_508dpi_LZW/SMR25_LV03_KOMB_Mosaic.tif") # sample data from [here][2]

# turn raster into data.frame and copy the colortable
map.df <- data.frame(rasterToPoints(map))
colTab <- colortable(map)

# give the colors their apropriate names:
names(colTab) <- 0:(length(colTab) - 1) 

# only define the colors used in the raster image
from <- min(map.df[[3]], na.rm = T)+1 
to <- max(map.df[[3]], na.rm = T)+1
used_cols <- colTab[from:to] 

# plot:
gplot(map, maxpixels = 5e5) +
  facet_wrap(~ variable) +
  geom_tile(aes(fill = value)) +
  scale_fill_gradientn(colours=used_cols) +
  coord_equal()

【讨论】:

  • 对于未来的用户,我会注意到这三种解决方案都不适合我。我不知道为什么;颜色选项卡总是充满了 NA。但是,我确实在这里找到了解决方案:gis.stackexchange.com/questions/102788/…。可以在 plotRGB() 之后使用 plot( , add=TRUE) 以便在 RasterStack 上分层 shapefile 对象。也可以为 RasterStack 分配一个范围并对其进行裁剪或遮罩,就像普通图层一样。
【解决方案3】:

我改进了解决方案并创建了一个小函数,允许直接导入 ggplot(带有将其转换为灰度的简洁选项)。

require(rasterVis)
require(raster)
require(ggplot2)

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

# Function to get the colourtable with the option of returing it in greyscale
getColTab <- function(rasterfile, greyscale = F){
  colTab <- raster::colortable(rasterfile)
  if(greyscale == T){
    colTab <- col2rgb(colTab)
    colTab <- colTab[1,]*0.2126 + colTab[2,]*0.7152 + colTab[3,]*0.0722
    colTab <- rgb(colTab,colTab,colTab, maxColorValue = 255)
  }
  names(colTab) <- 0:(length(colTab)-1)
  return(colTab)
}

gplot(map, maxpixels = 10e5) +
  geom_tile(aes(fill = factor(value))) +
  scale_fill_manual(values = getColTab(map),guide = "none") +
  coord_equal() 

【讨论】:

    猜你喜欢
    • 2021-09-17
    • 2013-04-10
    • 2012-03-04
    • 2023-03-31
    • 2022-08-13
    • 2019-01-20
    • 1970-01-01
    • 2021-08-20
    相关资源
    最近更新 更多