0

I'm attempting to learn how to import, display, and generally handle geo-tiff files in R-Studio and notebook. When I run the code I get no errors. The plot is not displayed but entering the plot name in the console gives an error. It's as if an error is detected, the plot is still created, but no error is reported either by running the chunks or by running 'knit'.

fimage_plot Error: Discrete value supplied to continuous scale

My code chunk:

rlist <- list.files(tiffTestFldrOrig, pattern="tif$",
                full.names=TRUE)
for(tiff_path_nane in rlist) {
  fimage <- raster(tiff_path_nane)
  fill_col_name = names(fimage)
  fimage_df <- as.data.frame(fimage, xy = TRUE)

  fimage_plot <- ggplot() +
    geom_raster(data = fimage_df, aes(x=x, y=y, 
              fill = fill_col_name)) +
    scale_fill_continuous(type = "gradient") +
    coord_quickmap()

  fimage_plot # no plot displayed, no error
  break() # until error corrected
}

I've tried google, searching on various scale_fill_discete, scale_fill_continous, etc. to no avail.

BTW my x & y data are UTM with the third column 16 bit integer values representing temperatures of a wildfire.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Nate Lockwood
  • 3,325
  • 6
  • 28
  • 34
  • 1
    Hi Nate, can you provide a minimal [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) example? – Majid Sep 17 '19 at 00:39
  • @Majid, I can, but how can I provide the 525 KB GEOTIFF data file? – Nate Lockwood Sep 17 '19 at 17:21
  • You may either `dput()` small portion of your data or make a minimal reproducible example as in [here](https://stackoverflow.com/questions/52658006/how-to-create-10-random-rasters-in-r?answertab=active#tab-top). I don't think that data needs to be exactly the same. – Majid Sep 18 '19 at 01:34

2 Answers2

2

OK @Nate I did something roughly by generating example data myself. Hope it works and that is what you wanted:

library(raster)
library(ggplot2)

r1 <- raster(nrows = 1, ncols = 1, res = 0.5, 
             xmn = -1.5, xmx = 1.5, ymn = -1.5, ymx = 1.5, vals = 0.3)
fimage <- lapply(1:10, function(i) setValues(r1,runif(ncell(r1))))
fimage_plot <- list()

for(i in 1:length(fimage)) {
  fimage_df <- as.data.frame(fimage[[i]], xy = TRUE)

  fimage_plot[[i]] <- ggplot(fimage_df, aes(x, y)) + 
    geom_raster(aes(fill = layer)) +
    guides(fill=guide_legend(title=paste0("Plot ", i))) # if you want to change the legend

  fimage_plot[[i]]  
  # break() # until error corrected
}
Majid
  • 1,836
  • 9
  • 19
  • I appreciate your help. See my comment to ibusett. I'm trying to learn R and to import and work with the data in GEOTIFF files. – Nate Lockwood Sep 18 '19 at 16:37
  • 1
    Whether or not the data is in geotiff files is not relevant to your question about using ggplot. This is how you set up a question --- without using files. – Robert Hijmans Sep 19 '19 at 18:35
  • One of the frustrating aspects of R is trying to figure out what the error messages mean. There was no error message emitted in building the plot, just in displaying it. In hindsight I see that a simple example would have excluded the file type as being relevant. – Nate Lockwood Sep 20 '19 at 17:50
1

This does not work because in:

geom_raster(data = fimage_df, aes(x=x, y=y, fill = fill_col_name))

you are using a character variable to specify fill. ggplot does not like that.

You can either avoid changing the names of fimage and then use

 geom_raster(data = fimage_df, aes(x=x, y=y, fill = layer)) 

as in @Majid reply, or use aes_string to associate the character variable to fill:

 geom_raster(data = fimage_df, aes_string(x="x", y="y", fill = fill_col_name)) 

(Note however that aes_string is soft deprecated: in the future it may stop working and you'll have to use tidy evaluation.)

HTH

lbusett
  • 5,801
  • 2
  • 24
  • 47
  • "ggplot doesn't like that" But it won't complain until until you try to display it. :-( I figured it out yesterday and guess I didn't attempt verify that it worked earlier since there were no errors when the code ran. What replaces the deprecated 'as_data_frame', I don't like to ignore warnings. – Nate Lockwood Sep 18 '19 at 21:35
  • `as_data_frame` is going to be replaced by `as_tibble`, I think. – lbusett Sep 19 '19 at 08:20
  • AFAIK 'as_tibble' can't handle raster layers, sigh, at least it crashed when I tried it. – Nate Lockwood Sep 20 '19 at 17:42