Re: [R] 2 barplots in the same graph

From: jia ding <dingjia_at_gmail.com>
Date: Fri 24 Feb 2006 - 21:17:55 EST

Thanks All!

I combine your answers and post the code here again, if later somebody need it.

Actually, what I want is:
> x1

[1] 1 2 10
> x2

[1] -3 5 -8
> barplot(x1,col="white",border="red")
> barplot(x2,col="white",border="green",add=T)
So that, the two plots even share the same x-axis.

But, It comes another question: >
barplot(x2,col="white",border="green",add=T), because there are 2 numbers are negative, some part of the bar is missing. Is there any automatic function to make the axes fit very well for both bars?

And, suppose I want to add another command: axis(1, 0:20) to draw x-axis. I notice it increase like: 0,1,2,3,...20; how can I make it 0,5,15,20?

Thanks!

On 2/22/06, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
>
> The barplot solution already presented is probably what you want
> but just in case here is a zoo solution:
>
> library(zoo)
> z <- merge(zoo(x2), zoo(x1, seq(x1)+.5))
> plot(z, type = "h", plot.type = "single", col = 1:2, lwd = 5)
>
> Or a similar solution without zoo:
> plot(c(x1, x2) ~ c(seq(x1)+.5, seq(x2)),
> col = c(1+0*x1, 2+0*x2), type = "h", lwd = 5)
>
>
> On 2/22/06, jia ding <dingjia@gmail.com> wrote:
> > Hello,
> >
> > I have a very simple question about "2 barplots in the same graph".
> >
> > It seems quite easy, but I searched google for long time, haven't find
> > solution.
> >
> > For example, I want one graph like:
> > x1=seq(0,2,by=0.3)
> > x2=seq(3,0,by=-0.1)
> > barplot(x1,col="red")
> > barplot(x2,col="green")
> >
> > It means if it's on the same graph, some bars are overlaped.
> > So if the bars are hollow, instead of filled with color, it will be
> better.
> >
> > Actually, I think it's something similar with matlab's "hold on"
> command.
> >
> > Thanks!
> >
> > Nina
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help@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
> >
>

        [[alternative HTML version deleted]]



R-help@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 Received on Fri Feb 24 21:26:18 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:45 EST