Ask Your Question
2

How can geom_raster be used to display missing values with an extra color coding?

asked 2023-06-25 06:15:04 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-06-25 06:41:02 +0000

pufferfish gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-25 06:15:04 +0000

Seen: 11 times

Last updated: Jun 25 '23