2017-02-20 14 views
2

私は解決策を探していましたが、既に存在する問題のどれも私の問題に合致しません。 まず:私は2つの変更を必要とするR:barpot、ggplot、plotlyの積み重なった棒グラフ

barplot(as.matrix(df[,4:6]) , 
    main="tobefound", horiz = FALSE,width = 1, 
    names.arg=colnames(df[,4:6]), 
    las=2, 
    col = c("blue", "red"), 
    legend = df[,3], 
    args.legend = list(x="topleft"), 
    beside= FALSE) 

BARPLOT

enter image description here

に:私はプロットしようとしています

Pat <- c(1,1,1,1,1,1,2,2,2,2,2,2) 
V_ID <- c(1,1,6,6,9,9,1,1,6,6,9,9) 
T_ID <- c("A","B","A","B","A","B","A","B","A","B", "A","B") 
apples <- c(1,1,1,1,1,1,1,1,1,1,1,1) 
bananas <- c(2,2,2,2,2,2,2,2,2,2,2,2) 
cranberries <- c(3,3,3,3,3,3,3,3,3,3,3,3) 

df <- data.frame(Pat,V_ID, T_ID, apples, bananas, cranberries) 

私はdata.frameを持っています私のすべてのすべての "B"(すべてのスタックの赤い部分)が一緒に積み重ねられ、次に青い部分が上に積まれるようにします。第二:私もplotly使用したソリューションやggplotを探しています

legend = df[1:2,3], 

を経由して、これを対処する以外にも一度だけAとBに凡例を減少させる方法があります。

おかげで、

+0

あなたが探しているもの[この](http://stackoverflow.com/questions/20349929/stacked-bar-plot-in-r?rq=1)ですか? – iled

+0

nope。前にあなたの提案を見ました。私の目標は、すべての "A"と "B"を一緒に色で積み重ねることです。 – Rivka

答えて

3

まずリシェイプ:

df_long <- tidyr::gather(df, 'key', 'value', apples:cranberries) 

次にプロット:国境のない

ggplot(df_long, aes(key, value, fill = T_ID)) + geom_col(col = 'black') 

enter image description here

それとも:

あなたが最初 T_IDdfをソートするために必要な基本グラフィックスを使用し

enter image description here

+0

まさに私が探していたものです。ありがとうございました ! – Rivka

3

df = df[order(df$T_ID), ] 

barplot(as.matrix(df[,4:6]) , 
     main="tobefound", horiz = FALSE,width = 1, 
     names.arg=colnames(df[,4:6]), 
     las=2, 
     ylim = c(0,40), 
     col = 1+as.numeric(as.factor(df$T_ID)), 
     border = NA, 
     beside= FALSE) 

box() 
legend('topleft', fill = 1+as.numeric(as.factor(levels(df$T_ID))), legend = levels(as.factor(df$T_ID))) 

enter image description here

+0

また、非常に良い、ありがとう。 – Rivka

関連する問題