7

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.

Community
  • 1
  • 1
Ratnanil
  • 1,641
  • 17
  • 43
  • Using your example, I get a colortable (266 values) in `map@legend@colortable`. What are your OS and your `sessionInfo`? –  Oct 27 '15 at 06:23
  • @Pascal: It must have been late yesterday.. `map@legend@colortable` works, and so does `colortable(map)`. – Ratnanil Oct 27 '15 at 09:57

3 Answers3

7

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

  • Thanks for this solution! The thing is, I have two issues: 1. You manually set the fill using `scale_fill_gradient(low = 'white', high = 'black')`. This will not work when I have a coloured map. Issue 2: I would really like to work with ggplot rather than gplot since I have to add additional data to the basemap. – Ratnanil Oct 27 '15 at 09:58
  • `gplot` is a wrap for `ggplot`, if you read the help page... Why do you think `ggplot2` is loaded? –  Oct 27 '15 at 10:02
  • OK my bad, I'm sorry :-/ Leaves issue 1 though.. I'm trying to solve it using the [linked SO question / answer by Florian R. Klein](http://stackoverflow.com/questions/19196923/r-original-colours-of-georeferenced-raster-image-using-ggplot2-and-raster-pac) I will edit / rephrase my original question. – Ratnanil Oct 27 '15 at 10:10
  • Works like a charm on the sample data, thank you! Oddly, it doesn't work quite right on the data that I'm working with, which is an older version of the sample data I provided. So I'll leave my answer there for anybody needing my slightly longer solution. – Ratnanil Oct 27 '15 at 11:55
1

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()
Community
  • 1
  • 1
Ratnanil
  • 1,641
  • 17
  • 43
  • 1
    For future users, I would note that none of these 3 solutions worked for me. I'm not sure why; the color tab was invariably filled with NAs. However, I did find a solution here: https://gis.stackexchange.com/questions/102788/r-plot-raster-with-real-colors. It's possible to use plot( , add=TRUE) after plotRGB() in order to layer shapefile objects over the RasterStack. It's also possible to assign the RasterStack an extent and to crop or mask it, as one would a normal layer. – Leah Bevis Jun 16 '20 at 17:17
1

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() 
Ratnanil
  • 1,641
  • 17
  • 43