我无法控制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))
给出了:
但我希望这些酒吧充满了颜色,所以我尝试了:
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
答案 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
一样。
答案 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))