scale_fill_manual定义NA值的颜色 [英] scale_fill_manual define color for NA values
本文介绍了scale_fill_manual定义NA值的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用ggplot2创建一个小节,并且在定义NA的颜色时遇到了一些问题.
I try to make a barplot with ggplot2 and am facing some issues with defining the color for NA.
ggh <- ggplot(data=dat, aes(x=var1, fill=var2))+
geom_bar(position="dodge")+
scale_fill_manual(
values=c("s"="steelblue", "i"="darkgoldenrod2", "r"="firebrick4", na.value="black"))
在我的var2中,我的值是c("s", "i", "r", NA)
.出于某种原因,我的scale_fill_manual
内部代码对NA不起作用,即使对其他所有值都可以正常工作.
In my var2 I have values c("s", "i", "r", NA)
. For some reason my code above inside the scale_fill_manual
does not work for NA, even if it works fine for all the others values.
有人可以帮我找出原因吗?
Can someone help me figure out why?
感谢您的帮助
推荐答案
na.value
必须在values
参数之外.这是一个示例:
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")
这篇关于scale_fill_manual定义NA值的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文