R plot background map from Geotiff with ggplot2

2019-02-07 13:17发布

问题:

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.

回答1:

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:



回答2:

I've improved the solution and created a little function that allows a direct import into ggplot (with the neat option of turing it into greyscale).

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


回答3:

Like I noted in my original question, I was able to solve the problem with Pascals input and this solution. This is the way the colors came out correctly:

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