0
votes

I want to draw a black border for every tile even though my data set is missing values for some combinations of x and y

library(dplyr)
set.seed(10)
df = data.frame(x=factor(sample(1:5, 5)), y=factor(sample(1:5, 5)), z=factor(sample(1:5, 5)))
ggplot(df, aes(x=x, y=y, fill=z)) + geom_tile(size=0.5, color="black") 

enter image description here

It will work if I fill NA for the missing xy values. Also, the legend now shows the "NA" label, which I want to hide.

Is there a way to draw every tiles without filling in NA?

# fill in NA for missing combinations of x and y before drawing geom_tile
df2 = df %>% complete(x,y)
ggplot(df2, aes(x=x, y=y, fill=z)) + geom_tile(size=0.5, color="black") + scale_fill_brewer(palette="Set1", na.value="white")

enter image description here

1

1 Answers

1
votes

I'm finding other SO posts about removing NA from legends by just filtering out the NA rows. In this case, it's clearly intentional that those NAs are there. Instead, you can set the breaks for the fill scale to include just the numbers you need and not the NAs.

ggplot(df2, aes(x = x, y = y, fill = z)) + 
  geom_tile(size = 0.5, color = "black") +
  scale_fill_discrete(na.value = "white", breaks = 1:5)

If you need something more robust, like if the z-scale isn't just a simple count of 1 to 5, you could pull out a vector of unique values to for variable, excluding NA, and pass that to the breaks argument.