2

3 つの期間にわたる 2 つのコホートの積み上げ棒グラフを表す必要があるデータ セットがあります。現在、私は年ごとにファセットを作成し、DV の確率値に基づいて入力しています (誰かが老人ホームに行く回数 t; pr that t=0, t=1, ... t >= 5) . 「比較」バーのそれぞれが黄色のグラデーションで塗りつぶされ、処理バーが青色のグラデーションで塗りつぶされるように、別のカラースケールを導入できるかどうかを調べようとしています. これを行う最善の方法は、2 つのプロットを重ね合わせることだと思いますが、ggplot (または他のパッケージ) でこれを行うことができるかどうかはわかりません。コードとスクリーンショットは以下のとおりです。

         tempPlot <- ggplot(tempDF,aes(x = HBPCI, y = margin,  fill=factor(prob))) + 
      scale_x_continuous(breaks=c(0,1), labels=c("Comparison", "Treatment"))+
      scale_y_continuous(labels = percent_format())+
      ylab("Prob snf= x")+
      xlab("Program Year")+
      ggtitle(tempFlag)+
      geom_bar(stat="identity")+
      scale_fill_brewer(palette = "Blues")+ #can change the color scheme here.
      theme(axis.title.y =element_text(vjust=1.5, size=11))+
      theme(axis.title.x =element_text(vjust=0.1, size=11))+
      theme(axis.text.x = element_text(size=10,angle=-45,hjust=.5,vjust=.5))+
      theme(axis.text.y = element_text(size=10,angle=0,hjust=1,vjust=0))+
      facet_grid(~yearQual, scales="fixed")

ここに画像の説明を入力

4

1 に答える 1

4

使用を検討することをお勧めしますinteraction()-- ここに再現可能な解決策があります:

year <- c("BP", "PY1", "PY2")
type <- c("comparison", "treatment")

df <- data.frame(year = sample(year, 100, T),
                 type = sample(type, 100, T),
                 marg = abs(rnorm(100)),
                 fact = sample(1:5, 100, T))
head(df)
#   year       type      marg fact
# 1   BP comparison 0.2794279    3
# 2  PY2 comparison 1.6776371    1
# 3   BP comparison 0.8301721    2
# 4  PY1  treatment 0.6900511    1
# 5  PY2 comparison 0.6857421    3
# 6  PY1  treatment 1.4835672    3

library(ggplot2)

blues <- RColorBrewer::brewer.pal(5, "Blues")
oranges <- RColorBrewer::brewer.pal(5, "Oranges")

ggplot(df, aes(x = type, y = marg, fill = interaction(factor(fact), type))) +
  geom_bar(stat = "identity") +
  facet_wrap(~ year) +
  scale_fill_manual(values = c(blues, oranges))

プロット

于 2015-10-30T15:26:24.417 に答える