geom_bar的颜色

时间:2012-09-12 07:50:37

标签: r ggplot2

我无法控制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))

给出了: enter image description here

但我希望这些酒吧充满了颜色,所以我尝试了:

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

2 个答案:

答案 0 :(得分:2)

首次尝试时使用fill=Imputed代替colour=Imputed

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))

您可以在fill=Imputed中设置geom_bar,但是您必须将其打包到aes,就像调用ggplot一样。

enter image description here

答案 1 :(得分:2)

使用colour = Imputed

而不是在原始美学映射中使用fill = Imputed
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))