我问了一个关于这里解决的热图的问题:custom colored heatmap of categorical variables。我按照接受的答案中的建议为所有组合定义了scale_fill_manual
。
基于这个问题,我想知道如何告诉ggplot2
使用所有变量组合绘制热图,而不仅仅是数据框中可用的热图(假设它们已经在{ {1}}但未在最终情节中显示。)
我该怎么做?
当前的绘图代码:
scale_fill_manual
scale_fill_manual包含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"))
及其各自颜色的所有组合。
答案 0 :(得分:0)
与@aosmith类似,我尝试expand.grid
来获得一组有限的组合,但tidyr::complete()
也很好用。添加颜色和字母,并使用设定的颜色范围进行填充。
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"))