1

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
4

2 に答える 2

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_baraesggplot

ここに画像の説明を入力

于 2012-09-12T08:00:40.510 に答える
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))
于 2012-09-12T08:00:43.787 に答える