Reputation: 81
I need to create one-row heatmap, that occasionally will be derived from NA-only column, but has to be displayed anyway. In the example below generating heatmap from p2 column will render "Error: Must request at least one colour from a hue palette.". Is there any way to force ggplot to display an "empty" heatmap?
library(ggplot2)
id <- letters[1:5]
p1 <- factor(c(1,NA,2,NA,3))
p2 <- factor(c(NA,NA,NA,NA,NA))
dat <- data.frame(id=id, p1=p1, p2=p2)
ggplot(dat, aes(x=id,y="identity")) + geom_tile(aes(fill = p1), colour = "white") #works fine
ggplot(dat, aes(x=id,y="identity")) + geom_tile(aes(fill = p2), colour = "white") #renders error
Upvotes: 3
Views: 302
Reputation: 5689
I think if you explicitly tell ggplot
how to handle NA
values using the na.value
inside a scale_fill_manual
call. This should take care of your issue, or at least head you in the right direction:
ggplot(dat, aes(x=id,y="identity")) +
geom_tile(aes(fill = p2), colour = "white") +
scale_fill_manual(values = "white",
na.value = "black")
You can change the values
argument to better handle the colors you like
Upvotes: 5