本文介绍了geom_bar的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法控制ggplot中条形图的颜色
I am having trouble controlling the colour of barplots in ggplot
require(mice)
require(ggplot2)
impute <- mice(nhanes, seed = 101)
ldt <-complete(impute,"long", include=TRUE)
ldt$Imputed<-ifelse(ldt$".imp"==0,"Observed","Imputed")
ggplot(ldt[!is.na(ldt$hyp),], aes(x= factor(hyp), colour=Imputed)) +
geom_bar() +
facet_wrap(~.imp, nrow = 1) +
scale_y_continuous(expand = c(0,0))
哪个给:
但是我希望条形充满颜色,所以我尝试了:
But I would like the bars to be filled with the colour, so I tried:
ggplot(ldt[!is.na(ldt$hyp),], aes(x= factor(hyp))) +
geom_bar(colour = Imputed) +
facet_wrap(~.imp, nrow = 1) +
scale_y_continuous(expand = c(0,0))
但这会导致错误:
Error in do.call("layer", list(mapping = mapping, data = data, stat = stat, :
object 'Imputed' not found
推荐答案
第一次尝试使用 fill = Impted
代替 colour = Impted
.
ggplot(ldt[!is.na(ldt$hyp),], aes(x= factor(hyp), fill=Imputed)) +
geom_bar() +
facet_wrap(~.imp, nrow = 1) +
scale_y_continuous(expand = c(0,0))
您可以改为在 geom_bar
中设置 fill = Imputed
,但是您必须将其包装在对 aes
的调用中将会调用 ggplot
.
You could set fill=Imputed
in geom_bar
instead, but you'd have to wrap it in a call to aes
, as you would in the call to ggplot
.
这篇关于geom_bar的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!