Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To display missing values with an extra color coding using geomraster in R, you can assign a specific color to the missing values using the scalefill_manual function by specifying the colors for non-missing values and the color for missing values separately.

For example, consider the following code snippet:

library(ggplot2)
library(reshape2)

# create a sample dataset with some missing values
df <- data.frame(x = rep(letters[1:5], each = 5),
                 y = rep(1:5, times = 5),
                 z = c(1, 2, 3, NA, 4,
                       2, 4, 1, 5, 2,
                       3, 5, NA, 1, 3,
                       4, 1, 5, 3, 1,
                       5, 3, 2, 4, NA))

# convert the dataset to wide format
df_wide <- dcast(df, y ~ x, value.var = "z")

# specify the two colors for the heat map
color_palette <- c("orange", "white")

# create the heatmap
ggplot(data = df_wide, aes(x = Var1, y = Var2, fill = z)) +
  geom_raster() +
  scale_fill_manual(values = color_palette, na.value = "grey") +
  theme_minimal()

In this code, we first create a sample dataset with some missing values (denoted by NA). We then convert the dataset to wide format using the dcast function from the reshape2 package. This allows us to create a heat map using the geom_raster function.

To specify the two colors for the heat map, we create a color palette with the two colors we want to use (orange and white). We then use the scalefillmanual function to specify the color palette for non-missing values and the color (grey) to use for missing values.

The resulting heatmap will display the non-missing values using the orange-white color palette and the missing values using the grey color.