Search code examples
rggplot2raster

ggplot creates a white background around the raster image


I have couple of issues with plotting raster data with ggplot.

(1) It creates a white background around the plot. I dont want the white border around the image.

(2) The NA values in the plot should be shown in the legend as missing values.

 X      Y           Z
1  75.875 12.375   2.6510951
2  76.125 12.375   5.4310212
3  76.375 12.375   3.6985113
4  76.625 12.375  -3.2694589
5  76.875 12.375  -1.6689374
6  75.875 12.125  -9.1670256
7  76.125 12.125  -4.7482244
8  76.375 12.125   1.1797042
9  76.625 12.125   0.1593383
10 76.875 12.125  -3.2102890
11 75.875 11.875 -11.0326909
12 76.125 11.875  -7.9738824
13 76.375 11.875  -0.6844057
14 76.625 11.875  -2.8986147
15 76.875 11.875  -0.1624192
16 75.875 11.625          NA
17 76.125 11.625  -2.3323547
18 76.375 11.625   8.6410179
19 76.625 11.625  -2.3619503
20 76.875 11.625          NA

  library(ggplot2)
  library(raster)
  library(cowplot)
  library(scales)

scale = range(XYZ.1$Z,na.rm=TRUE)

   b = round(seq(scale[1],scale[2],length.out = 3),digits = 1)

   p1<-ggplot(XYZ.1)+ geom_raster(aes(X,Y, fill=Z)) +
      coord_equal()+theme_bw()+xlab("")+ylab("") +
      scale_fill_gradient2(low = "blue", high = "red", mid = "white",
                           midpoint = 0, na.value = "black",
                           name="",limits= c(scale[1],scale[2])) +
      scale_x_continuous(breaks=seq(75.75,77.0, 0.25), labels=c(paste(seq(75.75,77.0, 0.25),"°E", sep="")))+
      scale_y_continuous(breaks=seq(11.5,12.5,0.25), labels=c(paste(seq(11.5,12.5,0.25),"°N", sep=""))) +
      theme(axis.text.x = element_text(angle = 45, hjust = 1),panel.grid.major = element_blank(),
            panel.grid.minor = element_blank())+
      ggtitle("reference : gauge") +
      theme(plot.title = element_text(hjust = 0.5,size = 10, face = "plain"),legend.title=element_text(size=10),legend.text=element_text(size=9))+
      theme(plot.margin = unit(c(0,0,0,0), "in"))

Raster Image


Solution

  • Following the idea in the second solution shown in this answer, you can add a "dummy" layer of points to get a color legend to use for your NA values.

    It's a "dummy" layer because we add it but then remove it by setting the size of the points to 0. The point is mapping the color aesthetic to a string inside the aes of geom_point.

    We can then use scale_color_manual to set the name to whatever we want (I used "NA") and then make the background black via legend.key in theme.

    Using expand = c(0, 0) for the axis scales removes the panel border.

    Here is a basic idea of how this could look:

    ggplot(XYZ.1) + 
         geom_raster(aes(X, Y, fill = Z)) +
         coord_equal() +
         geom_point( aes(X, Y, color = ""), size = 0) +
         scale_fill_gradient2(low = "blue", high = "red", mid = "white",
                              midpoint = 0, na.value = "black",
                              name = "") +           
         scale_colour_manual(name = "NA", values = NA) +
         theme(legend.key = element_rect(fill = "black")) +
         scale_y_continuous(expand = c(0, 0) ) +
         scale_x_continuous(expand = c(0, 0) )
    

    enter image description here