[R] Barplot
Marc Schwartz (via MN)
mschwartz at mn.rr.com
Tue Oct 3 23:36:54 CEST 2006
Mohsen,
I had not seen a reply to your follow up yet and I have been consumed in
meetings and on phone calls.
On your first question, add two additional lines of code:
BL <- c(36.35, 36.91, 25.70, 34.38, 5.32)
LR <- c(1.00, 4.00, 6.00, 3.00, 0.50)
Q <- c(1.92, 0.00, 0.00, 1.92, 0.00)
# Get the bar midpoints in 'mp'
mp <- barplot(LR, main='LR Value', col='orange', border='black',
space=0.05, width=(BL), xlab='Length', ylab='LR')
# Write the LR and Q values below the bar midpoints
mtext(1, at = mp, text = sprintf("%.1f", LR), line = 1)
mtext(1, at = mp, text = sprintf("%.1f", Q), line = 0)
# Write labels at minimum value of X axis
mtext(1, at = par("usr")[1], text = "LR", line = 1)
mtext(1, at = par("usr")[1], text = "Q", line = 0)
See ?par for more information.
With respect to adding some sort of curve fit/density plot to your data,
it is not clear to me what the data represents, as the x axis does not
appear to be monotonic in Q (other than the bar midpoints) and the y
axis values do not appear to be counts.
If you have the original vector of data, you may be better off with a
histogram rather than a barplot, since the histogram will enable a
common density area within the bars (ie. the area of the bars = 1.0)
over which you can then draw a normal density curve. This general
approach was recently covered here:
https://stat.ethz.ch/pipermail/r-help/2006-September/113686.html
and there are similar examples in the archives.
See ?hist and ?truehist in the MASS package.
HTH,
Marc Schwartz
On Mon, 2006-10-02 at 16:42 -0400, Mohsen Jafarikia wrote:
> Thanks for your response. I just have two more questions:
> 1) I don't know how to write the titles of the LR and Q behind their
> lines of values (at the bottom of the graph). I tried to write like
> text = sprintf("LR%.1f", LR)...
> but it writes 'LR' behind all values while I only want it once at the
> beginning of the line while all the LR and Q values are still in the mid
> points of bars.
>
> 2) I would like a line which connects the mid points of each bar to be
> like a density function (or regression) line which is not sharp like what I
> have now. I tried to write density in the code but it tells "Error in
> xy.coords(x, y) : 'x' and 'y' lengths differ"
> I appreciate any comment about these questions
>
> Thanks,
> Mohsen
>
>
> On 10/2/06, Marc Schwartz (via MN) <mschwartz at mn.rr.com> wrote:
> >
> > On Mon, 2006-10-02 at 11:14 -0400, Mohsen Jafarikia wrote:
> > > Hello,
> > >
> > > I have used the following data to draw my barplot:
> > >
> > > BL LR Q
> > >
> > > 36.35 1.00 1.92
> > > 36.91 4.00 0.00
> > > 25.70 6.00 0.00
> > > 34.38 3.00 1.92
> > > 05.32 0.50 0.00
> > >
> > > BL<-c(36.35, 36.91, 25.70, 34.38, 05.32)
> > > LR<-c(1.00, 4.00, 6.00, 3.00, 0.50)
> > > Q<-<(1.92, 0.00, 0.00, 1.92, 0.00)
> > >
> > > barplot(dt$LR, main='LR Value', col='orange', border='black', space=
> > 0.05,
> > > width=(dt$BL), xlab='Length', ylab='LR')
> > >
> > > axis(1)
> > >
> > > I would like to do the following things that I don't know how to do it:
> > >
> > > 1) Writing the value of each 'BL' on my X axis.
> > > 2) Writing the value of 'Q' on the bottom of X axis.
> > > 3) Draw a line on the bars which connects the 'LR' values.
> > >
> > > I appreciate your comments.
> > >
> > > Thanks,
> > > Mohsen
> >
> >
> > I'm not sure if I am getting this completely correct, but is this what
> > you want?
> >
> >
> > BL <- c(36.35, 36.91, 25.70, 34.38, 5.32)
> > LR <- c(1.00, 4.00, 6.00, 3.00, 0.50)
> > Q <- c(1.92, 0.00, 0.00, 1.92, 0.00)
> >
> >
> > # Get the bar midpoints in 'mp'
> > mp <- barplot(LR, main='LR Value', col='orange', border='black',
> > space=0.05, width=(BL), xlab='Length', ylab='LR')
> >
> > # Write the LR and Q values below the bar midpoints
> > mtext(1, at = mp, text = sprintf("%.1f", LR), line = 1)
> > mtext(1, at = mp, text = sprintf("%.1f", Q), line = 0)
> >
> > # Now connect the LR values across the bars
> > lines(mp, LR)
> >
> >
> > See ?barplot, ?mtext, ?sprintf and ?lines
> >
> > HTH,
> >
> > Marc Schwartz
> >
> >
> >
>
>
> ______________________________________________
> 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.
More information about the R-help
mailing list