[R] Stacked Bar

Jim Lemon jim at bitwrit.com.au
Tue Aug 21 13:55:26 CEST 2007


Deb Midya wrote:
> Hi R Users!
>  
> Thanks in advance.
>  
> I am using R-2.5.1 on Windows XP.
>  
> I am trying to do a stacked bar plot, but could not get through the following problem. The code is given below.
>    
>   1. How can I provide 15 different colors for each method with 15 Rows?
>    
>   2. How can I put the legend in a particular position (eg., in the top or bottom or right or left)? How can I put legend using a number of rows (eg., using two or three rows)? 
>   
Hi Deb,
As you have probably noticed, the integer coded colors repeat too 
quickly for the number of colors you want. You can use the rainbow()
function to generate colors like this:

barplot(x,beside=FALSE,col=rainbow(nrow(x)))

or there are lots of other color generating functions in the grDevices 
or plotrix packages. Here's how to get your legend in an empty space for 
your plot. There is also an emptyspace() function in the plotrix package 
that tries to find the biggest empty space in a plot, although it 
probably wouldn't work in this case.

legend(0,1000,rownames(x),fill=rainbow(nrow(x)))

Jim



More information about the R-help mailing list