Hi r-users, I have this data below and would like to plot a barplot overlap with a line. This is my data:> hist_50pdf_obs pdf_gen.50 1 0.000000 0.000000 2 0.083156 0.125366 3 0.132196 0.158230 4 0.126866 0.149432 5 0.120469 0.127897 6 0.121535 0.104096 7 0.103412 0.082171 8 0.082090 0.063539 9 0.065032 0.048408 10 0.050107 0.036470 11 0.036247 0.027236 12 0.031983 0.020198 13 0.017058 0.014893 14 0.009595 0.010928 15 0.007463 0.007986 16 0.006397 0.005816 17 0.003198 0.004222 18 0.003198 0.003057 19 0.000000 0.002208 I tried sq <- seq(0,900,by=50) sq.50 <- as.character(sq) barplot(t(hist_50[,1]), col= "blue", beside=TRUE, ylim=c(0,0.2), main="Observed and generated gamma sum", xlab="Rainfall totals(mm)",cex.axis=0.8,ylab="Probability") legend("topright", c("observed","generated"), fill= c("blue","yellow")) rownames(hist_50) <- sq.50 lines(spline(hist_50[,2]),lty=1) The problem is the x-axis label is invisible, I want 0, 50, 100,.... Another thing is I want the line to plot on top of the barplot. It seems that it is shifted to the left. Thank you. [[alternative HTML version deleted]]
Hi Roslina In order to get the X coordinates of your barplot, assign the barplot to a variable: x.coor <- barplot(something) Now use the x.coor for your line plot. Cheers, Tal ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Thu, Apr 1, 2010 at 1:44 AM, Roslina Zakaria <zroslina@yahoo.com> wrote:> Hi r-users, > > I have this data below and would like to plot a barplot overlap with a > line. > This is my data: > > hist_50 > pdf_obs pdf_gen.50 > 1 0.000000 0.000000 > 2 0.083156 0.125366 > 3 0.132196 0.158230 > 4 0.126866 0.149432 > 5 0.120469 0.127897 > 6 0.121535 0.104096 > 7 0.103412 0.082171 > 8 0.082090 0.063539 > 9 0.065032 0.048408 > 10 0.050107 0.036470 > 11 0.036247 0.027236 > 12 0.031983 0.020198 > 13 0.017058 0.014893 > 14 0.009595 0.010928 > 15 0.007463 0.007986 > 16 0.006397 0.005816 > 17 0.003198 0.004222 > 18 0.003198 0.003057 > 19 0.000000 0.002208 > > I tried > sq <- seq(0,900,by=50) > sq.50 <- as.character(sq) > barplot(t(hist_50[,1]), col= "blue", beside=TRUE, ylim=c(0,0.2), > main="Observed and generated gamma sum", > xlab="Rainfall totals(mm)",cex.axis=0.8,ylab="Probability") > legend("topright", c("observed","generated"), fill= c("blue","yellow")) > rownames(hist_50) <- sq.50 > lines(spline(hist_50[,2]),lty=1) > > The problem is the x-axis label is invisible, I want 0, 50, 100,.... > Another thing is I want the line to plot on top of the barplot. It seems > that it is shifted to the left. > > Thank you. > > > > > [[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. > >[[alternative HTML version deleted]]
Look at the 1st example on the help page for the updateusr function in the TeachingDemos package for a way to align a barplot and information added to it. See the axis function for creating your own custom axis. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Roslina Zakaria > Sent: Wednesday, March 31, 2010 4:44 PM > To: r-help at r-project.org > Subject: [R] barplot and line > > Hi r-users, > > I have this data below and would like to plot a barplot overlap with a > line. > This is my data: > > hist_50 > ??? pdf_obs pdf_gen.50 > 1? 0.000000?? 0.000000 > 2? 0.083156?? 0.125366 > 3? 0.132196?? 0.158230 > 4? 0.126866?? 0.149432 > 5? 0.120469?? 0.127897 > 6? 0.121535?? 0.104096 > 7? 0.103412?? 0.082171 > 8? 0.082090?? 0.063539 > 9? 0.065032?? 0.048408 > 10 0.050107?? 0.036470 > 11 0.036247?? 0.027236 > 12 0.031983?? 0.020198 > 13 0.017058?? 0.014893 > 14 0.009595?? 0.010928 > 15 0.007463?? 0.007986 > 16 0.006397?? 0.005816 > 17 0.003198?? 0.004222 > 18 0.003198?? 0.003057 > 19 0.000000?? 0.002208 > > I tried > sq <- seq(0,900,by=50) > sq.50 <- as.character(sq) > barplot(t(hist_50[,1]), col= "blue", beside=TRUE, ylim=c(0,0.2), > main="Observed and generated gamma sum", > xlab="Rainfall totals(mm)",cex.axis=0.8,ylab="Probability") > legend("topright", c("observed","generated"), fill= c("blue","yellow")) > rownames(hist_50) <- sq.50 > lines(spline(hist_50[,2]),lty=1) > > The problem is the x-axis label is invisible, I want 0, 50, 100,.... > Another thing is I want the line to plot on top of the barplot.? It > seems that it is shifted to the left. > > Thank you. > > > > > [[alternative HTML version deleted]]
Hi Roslina, here is a simple example: barplot.x.location <- barplot(c(12:17)) points(x = barplot.x.location, y = rep(10, length(aa)), col = 2, pch = 7) ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Thu, Apr 1, 2010 at 2:51 PM, Roslina Zakaria <zroslina@yahoo.com> wrote:> Hi Tal, > > I tried but didn't really understand what you mean. Can you give me an > example? > > Thank you. > > > > > --- On *Thu, 4/1/10, Tal Galili <tal.galili@gmail.com>* wrote: > > > From: Tal Galili <tal.galili@gmail.com> > Subject: Re: [R] barplot and line > To: "Roslina Zakaria" <zroslina@yahoo.com> > Cc: r-help@r-project.org > Date: Thursday, April 1, 2010, 6:03 PM > > Hi Roslina > > In order to get the X coordinates of your barplot, assign the barplot to a > variable: > > x.coor <- barplot(something) > > Now use the x.coor for your line plot. > > Cheers, > Tal > > > > ----------------Contact > Details:------------------------------------------------------- > Contact me: Tal.Galili@gmail.com<http://us.mc587.mail.yahoo.com/mc/compose?to=Tal.Galili@gmail.com>| 972-52-7275845 > Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | > www.r-statistics.com (English) > > ---------------------------------------------------------------------------------------------- > > > > > On Thu, Apr 1, 2010 at 1:44 AM, Roslina Zakaria <zroslina@yahoo.com<http://us.mc587.mail.yahoo.com/mc/compose?to=zroslina@yahoo.com> > > wrote: > >> Hi r-users, >> >> I have this data below and would like to plot a barplot overlap with a >> line. >> This is my data: >> > hist_50 >> pdf_obs pdf_gen.50 >> 1 0.000000 0.000000 >> 2 0.083156 0.125366 >> 3 0.132196 0.158230 >> 4 0.126866 0.149432 >> 5 0.120469 0.127897 >> 6 0.121535 0.104096 >> 7 0.103412 0.082171 >> 8 0.082090 0.063539 >> 9 0.065032 0.048408 >> 10 0.050107 0.036470 >> 11 0.036247 0.027236 >> 12 0.031983 0.020198 >> 13 0.017058 0.014893 >> 14 0.009595 0.010928 >> 15 0.007463 0.007986 >> 16 0.006397 0.005816 >> 17 0.003198 0.004222 >> 18 0.003198 0.003057 >> 19 0.000000 0.002208 >> >> I tried >> sq <- seq(0,900,by=50) >> sq.50 <- as.character(sq) >> barplot(t(hist_50[,1]), col= "blue", beside=TRUE, ylim=c(0,0.2), >> main="Observed and generated gamma sum", >> xlab="Rainfall totals(mm)",cex.axis=0.8,ylab="Probability") >> legend("topright", c("observed","generated"), fill= c("blue","yellow")) >> rownames(hist_50) <- sq.50 >> lines(spline(hist_50[,2]),lty=1) >> >> The problem is the x-axis label is invisible, I want 0, 50, 100,.... >> Another thing is I want the line to plot on top of the barplot. It seems >> that it is shifted to the left. >> >> Thank you. >> >> >> >> >> [[alternative HTML version deleted]] >> >> >> ______________________________________________ >> R-help@r-project.org<http://us.mc587.mail.yahoo.com/mc/compose?to=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]]