2012-01-10 65 views
0

值我使用以下代碼顏色根據堆barplotř

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

在這種情況下,我所得到的是在光譜的一端,底部和部分在在光譜的另一端頂部的部分。 由於

這是一些示例數據

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 2012-01-10 00:40:46

+0

謝謝@約翰,我希望它是一個線性關係。 – 2012-01-10 00:43:45

+1

有可能是一個優雅的ggplot2方式來做到這一點,我會添加一個標籤來吸引一些人羣的注意力。 – 2012-01-10 03:00:03

回答

1

barpplotrix包允許在顏色用於每個組和酒吧,所以你可以像它們指定一個矩陣通過,不同之處在於barp不會做堆疊的鏤空(即它只能做barplot(...,beside=TRUE)而不是barplot(...,beside=FALSE))。

或者,您可以使用rect分別用指定的顏色(!)繪製條形圖的每個矩形。

這是我設計的功能(修改,你當然需要它):

% 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) 
} 

示例輸出從您的測試數據與mybarplot(test)stacked bar graph with global colour scale

見盒的顏色是如何依賴於他們達到了多高。它是決定顏色的盒子的頂部,而不是底部。如果您想使用(比如說)框的底部來確定顏色,請相應地修改fracs行。

請注意,我至少會修改您的axis命令,因爲它們非常小且難以閱讀!

也許從功能忽略他們,但來自mybarplot返回xright[1:n]-(space+wid)/2並在xLabLocs <- mybarplot(test)使用,讓你提供額外的顯卡撥弄像axis外設功能的命令。

+1

注 - 如果任何人有一個解決方案,仍然會喜歡看「ggplot」解決方案! – 2012-01-10 04:01:01