2012-09-12 71 views
1

我無法控制barplots的顏色ggplot顏色geom_bar

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

在第一次嘗試時使用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)) 

您可以設置在geom_barfill=Imputed代替,但你必須在調用包裹,aes,就像在調用ggplot

enter image description here

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