Re: [R] stacked barplot with positive and negatvie values

From: Deepayan Sarkar <deepayan.sarkar_at_gmail.com>
Date: Tue, 15 May 2007 11:30:19 -0700

On 5/15/07, Marc Schwartz <marc_schwartz_at_comcast.net> wrote:
> On Tue, 2007-05-15 at 17:54 +0200, Paul Magdon wrote:
> > Hello I'm trying to create a barplot with a couple of stacked positive
> > values and with one negative value for each group.
> >
> > example:
> >
> > trees<-c(20,30,10)
> > shrubs<-c(12,23,9)
> > veg<-c(2,3,4)
> > soil<-c(-100,-123,-89)
> > example1<-t(cbind(trees,shrubs,veg))
> >
> > barplot(example1)
> >
> > #this works so far
> >
> > #but now:
> >
> > example2<-t(cbind(trees,shrubs,veg,soil))
> > barplot(example2)
> >
> > This shows no more stacked bars. But I want to keep the bars like
> > example1 and just add the negative values which have another scale
> > downwards.
> > So I tried:
> >
> > barplot(example1,axes=F)
> > barplot(example2["soil",],add=T,axes=F)
> > axis(side=2,at=c(-150,-100,-50,0,10,20,30))
> >
> > But I still does not work for the axis??
> >
> > I would appriciate any kind of hint
> > Greetings
> > Paul Magdon
>
>
> How about this:
>
> # Set up a 2 x 1 plot
> # See ?par
> par(mfrow = c(2, 1))
>
> #adjust the axis side 1 margin to 0
> # See ?par
> par(mar = c(0, 4, 4, 2))
>
> # Do the plot of the positive values
> # define the y limits and set the y axis type to 'i'
> # See ?par for 'xaxs' and 'yaxs'
> barplot(example2[-4, ], yaxs = "i", ylim = c(0, 60), las = 2)
>
> # Set the axis side 4 margin to 0
> # so that there is no gap
> par(mar = c(5, 4, 0, 2))
>
> # Now do the negative values, using the same logic
> barplot(example2[4, ], yaxs = "i", ylim = c(-125, 0), las = 2)

And another alternative is to use the lattice package, e.g.

barchart(Freq ~ Var2,

         data = as.data.frame.table(example2),
         groups = Var1, stack = TRUE, auto.key = TRUE)

-Deepayan



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. Received on Tue 15 May 2007 - 18:35:43 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 15 May 2007 - 20:33:41 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.