使用scale_fill在ggplot2中设置特定颜色

时间:2017-01-20 19:16:28

标签: r ggplot2

我使用this method使用ggplot2:

可视化R中的缺失数据
library(reshape2)
library(ggplot2)

ggplot_missing <- function(x){
  x %>% 
    is.na %>%
    melt %>%
    ggplot(data = .,
           aes(x = Var2,
               y = Var1)) +
    geom_raster(aes(fill = value)) +
    scale_fill_grey(name = "", labels = c("Present","Missing")) +
    theme_minimal() + 
    theme(axis.text.x  = element_text(angle=45, vjust=0.5)) + 
    labs(x = "Columns / Attributes",
         y = "Rows / Observations")
}

scale_fill_grey方法使用黑色和灰色。如何将细胞的颜色更改为特定颜色,例如&#34;红色&#34;?

我试过了:

 scale_fill_brewer(name = "", labels = c("Present","Missing"), na.val="red")

另外,

 scale_fill_gradient(name = "", labels = c("Present","Missing"), low = "#FF69B4", high = "#FF0000")

但我收到错误:

Error: Discrete value supplied to continuous scale

1 个答案:

答案 0 :(得分:0)

我通过将scale_fill_grey替换为以下内容来实现它:

scale_fill_manual(name = "", values = c('my_color_1', 'my_color_2'), labels = c("Present","Missing")) +