0

I asked a question about the heatmap which was solved here: custom colored heatmap of categorical variables. I defined my scale_fill_manual for all combinations as suggested in the accepted answer.

Based on this question, I would like to know how to tell ggplot2 to plot a heatmap with all combination of variables and not just the ones that are available in the dataframe (given that they are already in the scale_fill_manual but are not showing in the final plot).

How can I do this?

The current plotting code:

df <- data.frame(X = LETTERS[1:3], 
                 Likelihood = c("Almost Certain","Likely","Possible"), 
                 Impact = c("Catastrophic", "Major","Moderate"), 
                 stringsAsFactors = FALSE)
df$color <- paste0(df$Likelihood,"-",df$Impact)

ggplot(df, aes(Impact, Likelihood)) + geom_tile(aes(fill = color),colour = "white") + geom_text(aes(label=X)) +
  scale_fill_manual(values = c("Almost Certain-Catastrophic" = "red","Likely-Major" = "yellow","Possible-Moderate" = "blue"))

scale_fill_manual contains all combination of Impact, Likelihood with their respective colors.

ifreak
  • 1,726
  • 4
  • 27
  • 45
  • have a look at the `GGally` package. – Thierry Sep 14 '17 at 13:24
  • so you basically want all remaining tiles, for which there are no values, to be the same color (e.g., grey?) – timfaber Sep 14 '17 at 13:47
  • Could you add your current code to the question? One solution might be to expand the dataset to encompass all combinations of `Likelihood` and `Impact` prior to plotting with, e.g., `tidyr::complete`. – aosmith Sep 14 '17 at 15:02
  • no not the same color, basically based on the manual fill scale I provide for each combination @timfaber – ifreak Sep 15 '17 at 06:38
  • @aosmith code added – ifreak Sep 15 '17 at 06:41
  • I don't see a fill color for every possible combination of `Likelihood` and `Impact`. What should the other fill colors be? – aosmith Sep 15 '17 at 13:37
  • @aosmith yes, its because the list is too long to put here. But there is a color for each combination – ifreak Sep 15 '17 at 14:52
  • In that case, my idea above should work. You can expand your dataset so all combinations of the two variables are present prior to making the `fill` variable and plotting. That task of expanding the dataset is fairly straightforward with`tidyr::complete`. – aosmith Sep 15 '17 at 15:17

1 Answers1

0

Similar to @aosmith I tried expand.grid to get a finite set of combinations but tidyr::complete() works pretty nice as well. Add the colors and letters and fill using a set color range.

df <- data.frame(Likelihood = c("Almost Certain","Likely","Possible"), 
                 Impact = c("Catastrophic", "Major","Moderate"), 
                 stringsAsFactors = FALSE)
df2 <- df %>% tidyr::complete(Likelihood,Impact) # alt expand.grid(df)

df2$X <- LETTERS[1:9] # Add letters here
df2$color <- paste0(df2$Likelihood,"-",df2$Impact) # Add colors


ggplot(df2, aes(Impact, Likelihood)) + geom_tile(aes(fill = color),colour = "white") + geom_text(aes(label=X)) +
  scale_fill_manual(values = RColorBrewer::brewer.pal(9,"Pastel1"))
timfaber
  • 2,060
  • 1
  • 15
  • 17