2012-01-10 17 views
0

に、私は次のコード色値に応じてスタックbarplotのR

test <- as.matrix(read.csv(file="test4.csv",sep=",",head=TRUE)) 
test <- test[,2:ncol(test)] 
pdf(file="test.pdf", height=15, width=20) 
par(lwd = 0.3) 
xLabLocs <- barplot(test, space=0.4, xaxt='n', yaxt='n', col=heat.colors(13)) 
axis(1, cex.axis=0.5, las=2, at=xLabLocs, labels=colnames(test)) 
axis(2, cex.axis=0.5, pos=-0.5) 
dev.off() 

を用いた積層バープロットを構築していると私は、各セクションの色はその高さに比例するようにしたいと思います。たとえば、各スタックがX個のセクションで構成されている場合、最も長いセクションの長さは「スペクトル」の一端(つまり、実際には明るい青色)になり、最短の高さセクションは、 「スペクトル」(すなわち、実際には暗い青色)。

これは私が代わりに得るものです:私は何を得る。この場合 enter image description here

は、スペクトルのもう一方の端にスペクトルの一端の下部にあるセクションと上部のセクションです。 おかげ

これはplotrixパッケージからいくつかのサンプルデータ

BARCODES, BC1_AATCAGGC, BC10_ACAAGGCT, BC11_ACACGATC, BC12_ACACTGAC 
1, 2432, 420, 18, 69 
2, 276, 405, 56, 86 
3, 119, 189, 110, 51 
4, 90, 163, 140, 68 
5, 206, 280, 200, 122 
6, 1389, 1080, 1075, 614 
7, 3983, 3258, 4878, 2994 
8, 7123, 15828, 28111, 7892 
9, 8608, 48721, 52576, 21220 
10, 9639, 44725, 55951, 18284 
11, 8323, 45695, 32166, 7747 
12, 2496, 18254, 26600, 5134 
13, 1524, 8591, 18583, 3705 
+0

この回答を得るには、さらに詳しい情報を提供する必要があります。セクションの高さに応じて色を変えたいとしたら、線形の関係にしたいのですか、棒の中の最も長い部分がどのくらいの長さになっても常に同じ色になるように、ありますか?完全に再現可能なチャートを作成するデータの小さなサンプルは、ここで長い道のりになります。 – John

+0

ありがとう@ジョン、私は線形の関係にしたいと思います。 –

+1

おそらくこれを行うためのエレガントなggplot2の方法があります、私はその群衆の注目の一部を描画するタグを追加します。 –

答えて

1

barpでは、各グループのために使用しますので、ことを除いて、そのようにそれらを指定することができますバーする色のマトリックスに渡すことができますbarp積み重ねられた棒グラフはありません(すなわちbarplot(...,beside=TRUE)barplot(...,beside=FALSE)ではありません)。

また、rectを使用すると、棒グラフの各矩形を指定した色(!)で個別に描画できます。ここで

は、私が考案した機能は、(あなたはもちろん、それを必要とするよう変更)です:mybarplot(test)を使用してテストデータから

% mybarplot(x, col=heat.colors(255), space=0.2, labels=NULL) 
% makes a stacked bar plot with ncol(x) bars, each containing nrow(x) 
% stacks. Coloured according to a *global* colour scale (by value of top edge 
% of the box within the stack). This is as opposed to the same colour 
% per category across all bars. 
% 
% PARAMETERS 
% ---------- 
% x  : a matrix. COLUMNS are the categories, ROWS contain the data. 
% col : colour scheme to use. e.g. heat.colors, rainbow, ... 
% space : space between bars as a fraction of bar width. 
% labels: labels for each category (column) of x, default colnames(x) 
% 
% EXAMPLE 
% ------- 
% bar plot with 3 categories/bars, 4 stacks in each bar. 
% data <- matrix(runif(12),ncol=3,nrow=4) 
% colnames(data)<-c('group a','group b','group c') 
% mybarplot(data,col=heat.colors(20)) 
% 
mybarplot <- function(x, col=heat.colors(255), space=0.2, labels=colnames(x)) 
{ 
maxy <- max(x) 
miny <- 0 
n <- ncol(x) 
m <- nrow(x) 
wid <- 1 

# work out boundaries of each rectangle 
# note: sort the y's ascending to draw properly. 
xsort <- apply(x,2,sort) 
xright <- rep(1:n, m) * (wid+space) - space 
ybottom <- as.vector(t(rbind(miny,xsort))) 

# work out colour of each rectangle, 
# being (y/maxy) along the colour scale. 
fracs<-as.vector(t(xsort))/maxy 
cols <- col[round(fracs*(length(col)-1))+1] 

# plot: set up grid and then draw rectangles 
plot(0, 0, type="n", 
     ylim=c(miny,maxy), xlim=c(0,max(xright)), 
     xaxt='n',yaxt='n',xlab=NA,ylab=NA) 
rect(xright-wid, ybottom[1:(length(ybottom)-n)], xright, ybottom[-(1:n)], 
     col=cols) 

# draw labels 
axis(1, cex.axis=0.5, las=2, at=xright[1:n]-(space+wid)/2, labels=labels) 
axis(2, cex.axis=0.5, pos=-0.5) 
} 

出力例: stacked bar graph with global colour scale

は、箱の色が依存する方法を参照してください。彼らがどのくらい高いところに達するか。それは色を決定するボックスの最上部です。底部ではありません。代わりにボックスの下部を使用して色を決定する場合は、それに応じてfracs行を変更してください。

実際には小さくて読みにくいので、私は少なくともあなたのaxisコマンドを修正することを念頭に置いています。

おそらく機能からそれらを省略したがmybarplotからxright[1:n]-(space+wid)/2を返すと、関数のaxis外のような余分なグラフィックスコマンドをいじるすることができ、xLabLocs <- mybarplot(test)のように使用します。

+1

注 - もし誰かがいるなら、 'ggplot'の解決策を見たいと思うでしょう! –

関連する問題