2012-01-10 64 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