[R] Stacked Bar
Stephen Tucker
brown_emu at yahoo.com
Tue Aug 21 15:04:13 CEST 2007
I think you want to use the 'density' argument. For example:
barplot(1:5,col=1)
legend("topleft",fill=1,legend="text",cex=1.2)
par(new=TRUE)
barplot(1:5,density=5,col=2)
legend("topleft",fill=2,density=20,legend="text",bty="n",cex=1.2)
(if you wanted to overlay solid colors with hatching)
Here's the lattice alternative of the bar graph, though the help page says
'density' is currently unimplemented (Package lattice version 0.16-2). To get
the legend into columns, I followed the suggestion described here:
http://tolstoy.newcastle.edu.au/R/help/05/04/2529.html
Essentially I use mapply() and the line following to create a list with
alternating 'text' and 'rect' arguments (3 times to get 3 columns).
===
x <- matrix(1:75, ncol= 5)
dimnames(x)[[2]] <- paste("Method", 1:5, sep="")
dimnames(x)[[1]] <- paste("Row", 1:15, sep="")
u <- mapply(function(x,y) list(text=list(lab=x),rect=list(col=y)),
x = as.data.frame(matrix(levels(as.data.frame.table(x)$Var1),
ncol=3)),
y = as.data.frame(matrix(rainbow(nrow(x)),
ncol=3)),
SIMPLIFY=FALSE)
key <- c(rep=FALSE,space="bottom",unlist("names<-"(u,NULL),rec=FALSE))
barchart(Freq ~ Var2,
data = as.data.frame.table(x),
groups = Var1, stack = TRUE,
col=rainbow(nrow(x)),density=5,
key = key )
===
(I often use tim.colors() in the 'fields' package, if you wanted other ideas
for color schemes).
--- Deb Midya <debmidya at yahoo.com> wrote:
> Jim,
>
> Thanks for such a quick response. It works well. Is it possible to fill
> the bars with patterns and colours?
>
> Regards,
>
> Deb
>
> Jim Lemon <jim at bitwrit.com.au> wrote:
> 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
>
>
>
>
> ---------------------------------
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
____________________________________________________________________________________
Comedy with an Edge to see what's on, when.
More information about the R-help
mailing list