This graph would be easier under lattice graphics.

biomass <- data.frame(bg=c(0.41, 0.37, 0.31, 0.32),
                      ag=c(2.81, 2.91, 2.06, 2.39))
b2 <- stack(biomass)
names(b2) <- c("mass", "type")
b2$type <- factor(b2$type, levels=c("bg","ag"))
b2$AB <- rep(c("A","A","B","B"), 2)
b2$location <- rep(1:4, 2)
b2
barchart(mass ~ location | AB, group=type, data=b2,
         horizontal=FALSE, stack=TRUE,
         col=c("white", "grey90"),
         scales=list(x=list(relation="sliced")),
         ylim=c(0, 10),
         ylab="Total biomass (g)")

I did several things above.  First, I gave the data frame a name that has
some semblance
of what the data is (I don't know more about it, but you can do even better
with an appropriate name).

Second, I recognize that there are three factors---using the technical
definition of a factor.  You were using
the term factor very informally and inconsistently.  You said that there
are four factors and also that there are
two factors.  I interpret that as the factor location with 4 levels, and
the factor AB with two levels.
You also have the factor type with two levels, these are the columns of
your original data organization.
I invented the names location, AB, and type.  You can pick more appropriate
names.

Third, since you want to distinguish the AB levels, I did it graphically by
putting them in separate panels.

I suggest you learn lattice graphics.

Rich



On Mon, Jan 9, 2012 at 8:01 PM, acacia21 <chriss...@hotmail.com> wrote:

> Hi all,
> i'm fairly new to R and its graphing, but having unsuccessfully  'googled'
> and checked this forum to find answer to my problem, i'm posting my
> question
> here.
>
> I'm trying to plot stacked barplot. I have simple data that looks like
> this:
> bg               ag
> 0.41    2.81
> 0.37    2.91
> 0.31    2.06
> 0.32    2.39
>
> every row indicates a factor (1,2,3,4, see below in names.arg). Now when i
> plot this using following function for stacked barplots:
> plot<-barplot(t(data), main=txt, ylim=c(0,10), col=c("white", "grey90"),
> ylab="Total biomass (g)", space=0.1, names.arg=c("1", "2", "3", "4"))
>
> i get a lovely stacked graph and it color-codes with white (bg) and grey90
> (ag) in each individual stacked bar.  I have a total of 4 stacked bars as i
> have 4 factors. Now here is the question: i would like to add density lines
> across entire stacked bar or any other graphic feature to distinguish
> between bars 1 and 2 as they indicate same factor and  3 and 4 that
> indicate
> different factor. Is there any way to do that? Surely it's possible, but
> not
> so obvious for the beginner =)
>
> thank you very much
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/stacked-barplot-colour-coding-tp4280685p4280685.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org 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.

Reply via email to