scale_fill_manual define color for NA values

孤街醉人 提交于 2019-11-30 19:22:28

The na.value needs to be outside of the values argument. Here is an example:

library(ggplot2)

set.seed(42)

mydata <- data.frame(var1 = sample(c("A", "B", "C", "D"), 150, replace = TRUE),
                     var2 = sample(c("s", "i", "r", NA), 150, replace = TRUE))

ggplot(mydata) +
  aes(x = var1, fill = var2) +
  geom_bar() + 
  scale_fill_manual(values = c("s" = "steelblue",
                               "i" = "darkgoldenrod2",
                               "r" = "firebrick4"),
                    na.value = "black")

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!