0

I have rasterLayers that contain digital temperature data from an airborne IR camera. I can create a false color plot using ggplot. I would like to save the plot as a PNG with values of zero being transparent and/or create KML files for displaying on Google Earth.

I have been creating the PNG files in a loop with the code below. "heatMapPlot" is the function that uses ggplot to create the plot - it works. It sets na.value <- "#000000" so that I can detect if borders are added by print( p ) - they are.

  png(file = fname, width = NCOLS, height = NROWS)
  p <<- heatMapPlot(
    theRasterLayer[[passNum]], colors, colorvalues
  )
  print(p)
  dev.off()

If I plot p

> plot(p)

The result is exactly what I want but it is not saved.

Writing p to a file with print(p) adds a wide white border around 80 pixels wide around the plot and the plot is scaled to fit into the width and height. This is unacceptable for placing on Google Earth. The image below shows the output ggplot output in the black area, the white border was added by "print(p)". (Feels like auto correct)

How can I write the PNG to disk without the border and/or how can I convert the ggplot output to a KML?

EDIT I think this is not possible, I'll go straight to KML files but there is a palette problem.

Saved with print(p) See the added border in the image.

Nate Lockwood
  • 3,325
  • 6
  • 28
  • 34
  • Have you tried with `ggsave` ? – Stéphane Laurent May 07 '20 at 17:17
  • +1 on the `ggsave()` suggestion. If you still get the white border, you can also try to remove the plot margins via adding the following to your `ggplot` object: `p + theme(plot.margin=margin(0,0,0,0))` – chemdork123 May 07 '20 at 18:51
  • @chemdork123 Thanks, I did add that to my theme but print ignored it. I'll try ggsave() next but it appears to me that it's geared for publication, much like print() – Nate Lockwood May 07 '20 at 20:27
  • [Does this help](https://stackoverflow.com/questions/26034177/save-multiple-ggplots-using-a-for-loop)? Seems you should try using `png()` in your `for` loop instead of `print()` for a bit better control. – chemdork123 May 07 '20 at 20:30
  • @StéphaneLaurent That didn't work, alas, it the results were the same.. I think ggsave() is more for presentation – Nate Lockwood May 07 '20 at 21:26
  • @chemdork123 I couldn't get png() to work at all, no files create, no errors, time to complete the script about right. I suspect from the parameters that png() is for presentation. – Nate Lockwood May 08 '20 at 00:30

0 Answers0