Search code examples
rplotggplot2raster

R plot background map from Geotiff with ggplot2


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.


Solution

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

    enter image description here

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

    enter image description here

    With colors:

    enter image description here