[R] stacked barplot with positive and negatvie values
Marc Schwartz
marc_schwartz at comcast.net
Tue May 15 20:17:51 CEST 2007
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)
HTH,
Marc Schwartz
More information about the R-help
mailing list