[R] Stacked Bar
brown_emu at yahoo.com
Tue Aug 21 15:04:13 CEST 2007
I think you want to use the 'density' argument. For example:
(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:
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)[] <- paste("Method", 1:5, sep="")
dimnames(x)[] <- 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),
y = as.data.frame(matrix(rainbow(nrow(x)),
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,
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:
> Thanks for such a quick response. It works well. Is it possible to fill
> the bars with patterns and colours?
> 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:
> 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.
> [[alternative HTML version deleted]]
> R-help at stat.math.ethz.ch mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
Comedy with an Edge to see what's on, when.
More information about the R-help