Dear: I want to plot barplot and let bar be in the middle of each x axis category. Do you have this experience? Many Thanks! Xin [[alternative HTML version deleted]]
Duncan Murdoch
2008-Feb-16 12:46 UTC
[R] how to specify the location of tick mark on x axies
Xin wrote:> Dear: > > I want to plot barplot and let bar be in the middle of each x axis category. > > Do you have this experience? > > Many Thanks! > > Xin > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >> x <- rnorm(100) > h <- hist(x, axes=F) > str(h) List of 7 $ breaks : num [1:10] -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 $ counts : int [1:9] 2 7 23 17 28 9 10 2 2 $ intensities: num [1:9] 0.04 0.14 0.46 0.34 0.56 ... $ density : num [1:9] 0.04 0.14 0.46 0.34 0.56 ... $ mids : num [1:9] -1.75 -1.25 -0.75 -0.25 0.25 0.75 1.25 1.75 2.25 $ xname : chr "x" $ equidist : logi TRUE - attr(*, "class")= chr "histogram" > axis(1, at=h$mids) I'd say that graphically it's not the best idea, but it's not hard to do. If you want variations, see the manual pages ?hist, ?axis, etc. Duncan Murdoch
I think what you want for your last statement is: lines(pts, y2) This uses the value of the tick marks to plot your line. On Feb 16, 2008 6:53 AM, Xin <jasonshi510 at hotmail.com> wrote:> hi, > > I did barplot. My data are: > > >y1<-c(13, 20, 22, 19, 10, 16, 8, 4, 3, 5, 7, 4, 0, 4, 4, 2, 4, 2, 2, 5, 1) > >y2<-c(13, 23.29568698, 18.1385593, 14.97159795, 12.57640037, 10.65752306, > 9.079421331, 7.7625489, 6.653641903, 5.714125735, 4.914645265, > 4.232117758, 3.647980094, 3.147064034, 2.716830439, 2.346823055, > 2.02826436, 1.753747752, 1.516997668, 1.31267921, 1.136244845 > ) > >x<-c(0, 1, 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, > 76, 81, 86, 91, 96) > > >pts=barplot(y1,ylim=c(0,40),axes=TRUE,names.arg=x,border=TRUE,col="white") > >axis(side=1,at=pts, labels=F, tick=T) > > x axis with tickmarks exactly at the middle of the bars > > Then I want to add line into the barplot. I used > > >lines(x,y2) > > But the data points of the line is plotted at the beggining of each category > on x axis. I want to them plotted at the middle of each category. > > Can you help? > > Xin > > > > ----- Original Message ----- > From: "jim holtman" <jholtman at gmail.com> > To: "Xin" <jasonshi510 at hotmail.com> > Sent: Saturday, February 16, 2008 11:43 AM > Subject: Re: [R] how to specify the location of tick mark on x axies > > > > PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > > Can you provide an example of what you are doing and what you want. > > > > On Feb 16, 2008 6:14 AM, Xin <jasonshi510 at hotmail.com> wrote: > >> Dear: > >> > >> I want to plot barplot and let bar be in the middle of each x axis > >> category. > >> > >> Do you have this experience? > >> > >> Many Thanks! > >> > >> Xin > >> [[alternative HTML version deleted]] > >> > >> ______________________________________________ > >> R-help at 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. > >> > > > > > > > > -- > > Jim Holtman > > Cincinnati, OH > > +1 513 646 9390 > > > > What is the problem you are trying to solve? > > > >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Xin wrote:> Dear: > > I want to plot barplot and let bar be in the middle of each x axis category. > > Do you have this experience? >Hi Xin, The "barp" function in the plotrix package centers the bars on integer values. This might make it a bit easier to do what you want. Jim