2016-03-29 11 views
1

私の質問は、回答に加えて、4年前にhereと提出しました。ユーザーは、ドッジドスタックバーチャートを作成しました。どのように/各「結果」レベルごとにこれらのドッジを上に置くことが可能かどうかは疑問です。ここでggplot積み重ねられたカテゴリ内のアルファ差異を持つ積み重なった棒グラフ

は、ユーザーの答えとコードです:

enter image description here

を私がいることを知っている:ここで

library(ggplot2) 
library(plyr) 

N <- 50*(2*8*2) 
outcome <- sample(ordered(seq(8)),N,replace=TRUE,prob=c(seq(4)/20,rev(seq(4)/20))) 
category2 <- ifelse(outcome==1, sample(c("yes","not"), prob=c(.95,.05)), sample(c("yes","not"), prob=c(.35,.65))) 
dat <- data.frame(
    category1=rep(c("in","out"),each=N/2), 
    category2=category2, 
    outcome=outcome 
) 

# Aggregate 
dat.agg <- ddply(dat, .var = c("category1", "outcome"), .fun = summarise, 
       cat1.n = length(outcome), 
       yes = sum(category2 %in% "yes"), 
       not = sum(category2 %in% "not") 
) 


# Plot - outcome will be x for both layers 
ggplot(dat.agg, aes(x = outcome)) + 

    # First layer of bars - for category1 totals by outcome 
    geom_bar(aes(weight = cat1.n, fill = category1), position = "dodge") + 

    # Second layer of bars - number of "yes" by outcome and category1 
    geom_bar(aes(weight = yes, fill = category1), position = "dodge") + 

    # Transparency to make total lighter than "yes" - I am bad at colors 
    scale_fill_manual(value = c(alpha("#1F78B4", 0.5), alpha("#33A02C", 0.5))) + 

    # Title 
    opts(title = "A pretty plot <3") 

enter image description here

は、私がしようとしています何の極度の粗MSペイント図でありますopts()は廃止予定ですので、今後はそれを含めません。

バーを互いに重ね合わせるための最小値、最大値を設定する方法がわかりません。 position="dodge"position="stack"に変更するだけで、お互いに重なり合って理解できなくなります。私はまた、意味があれば、アウトラインを「入」と「アウト」の値ではなく、ALLの値の周りだけに保つことを好みます。ここで

+0

私は何かを明確にしたいと思います。 'outcome = 1'と' category = in'の 'dat.agg'の値を見ると、バーの高さは47ですが、" in "と" out "の合計高さは73です。積み重なった棒の高さを2つの合計にしたいと思うのですが、これは正しいのですか?どちらの場合でも、 'tidyr'を使って、以下のようなものを使ってデータを作り直したいと思うでしょう:' dat.agg.deep <- dat.agg %>%select(-not)%>%gather(val_type、val、 - category1、-outcome) 'となります。 'not'カラムはプロットしていないので削除されます。 'dat.agg.deep'をプロットします。 – steveb

答えて

2

は、溶融機能によりデータリシェイプを使用してコードです:

library(ggplot2) 
library(plyr) 

N <- 50*(2*8*2) 
outcome <- sample(ordered(seq(8)),N,replace=TRUE,prob=c(seq(4)/20,rev(seq(4)/20))) 
category2 <- ifelse(outcome==1, sample(c("yes","not"), prob=c(.95,.05)), sample(c("yes","not"), prob=c(.35,.65))) 
dat <- data.frame(
    category1=rep(c("in","out"),each=N/2), 
    category2=category2, 
    outcome=outcome 
) 

# Aggregate 
dat.agg <- ddply(dat, .var = c("category1", "outcome"), .fun = summarise, 
       cat1.n = length(outcome), 
       yes = sum(category2 %in% "yes"), 
       not = sum(category2 %in% "not") 
) 
plotData <- dat.agg[, c("category1", "outcome", "cat1.n", "yes")] 
plotData <- melt(plotData, id.vars = c("category1", "outcome")) 
plotData$FillColor <- ordered(paste0(plotData$category1, "_", plotData$variable), levels=c("in_cat1.n", "out_cat1.n", "in_yes", "out_yes")) # order it the way you want your values to be displayed on the plot 

# Plot - outcome will be x for both layers 
ggplot(plotData, aes(x = outcome)) + 

    # Add the layer 
    geom_bar(aes(weight = value, fill = FillColor)) + 

    # Add colors as per your desire 
    scale_fill_manual(values = c(alpha("#1F78B4", 1), alpha("#1F78B4", 0.5), alpha("#33A02C", 1), alpha("#33A02C", 0.5))) + 

    # Title 
    ggtitle("A pretty plot <3") 
関連する問題