[R] barplot2 x-axis
Marc Schwartz
marc_schwartz at comcast.net
Mon Mar 23 00:46:55 CET 2009
Looking at the graphic (I got it here, but it was stripped for those
getting it via the server), the CI's don't really add much to the
interpretation of the data. They are small enough as to be a non-issue
at least in this example and there is too much data to make them
useful for visually comparing the means across the bars anyway.
The values along the x axis that you are getting are the result of
there being too many small vertical bars in the space and that the
size of the font for the bars is too large to display all of the
labels. The other consideration, is that the horizontal spacing of the
bars will not be based upon their numeric value on a linear scale, but
their physical sequence in your data. Thus, if there are sequence gaps
in the distance measures along the x axis, those gaps will not be
maintained when displayed, resulting in a compromised linear scale
along the axis.
For example, consider:
x <- c(1:5, 8, 15:18)
names(x) = c(1:5, 8, 15:18)
barplot(x)
Note that there are gaps (6:7 and 9:14) where data is not present, but
the bars (5/8 and 8/15) are nevertheless next to each other. You would
need to insert NA's at the missing data points to maintain the proper
bar locations along the x axis.
Here is what it should look like:
x <- c(1:5, NA, NA, 8, rep(NA, 6), 15:18)
names(x) <- c(1:5, NA, NA, 8, rep(NA, 6), 15:18)
barplot(x, cex.names = 0.75)
Contrast how barplot() handles such data by default versus plot():
x <- c(1:5, 8, 15:18)
plot(x, x, type = "h", lwd = 10)
Recognizing that there may be community standards for you in how such
data (presumably animal or vegetative abundance data) should be
presented, my recommendation otherwise in a vacuum would be to plot
the data using points and leave out the CIs:
plot(mydata.x, mydata.y, xlab = "km", ylab = "kg", main = "plot.name",
xlim = c(0, 250), xaxt = "n", pch = 19)
axis(1, at = seq(0, 250, 25))
or if you need bars (use type = "h" as above), if that is the
preferred presentation format:
plot(mydata.x, mydata.y, xlab = "km", ylab = "kg", main = "plot.name",
xlim = c(0, 250), xaxt = "n", type = h", lwd = 5)
axis(1, at = seq(0, 250, 25))
The bottom line is the key take away message that you are trying to
convey, without that message being lost in too much ink.
HTH,
Marc
On Mar 22, 2009, at 4:45 PM, Mafalda Viana wrote:
> Dear Mark,
>
> Thanks for your reply.
>
> I attach one of my plots as an example (e.g.Mafalda) to show what I
> want. If
> you notice in the plot the x-axis has a funny scale (0, 13, 28...)
> and what
> I would like to do is to make that scale from 0 and then increasing
> by 25 km
> for a better interpretation (0, 25, 50...).
>
> Hopefully my problem is more clear now, meanwhile I will have a look
> to your
> suggestions.
>
> Thank you very much
> Mafalda
>
>
>
> 2009/3/22 Marc Schwartz <marc_schwartz at comcast.net>
>
>> On Mar 22, 2009, at 2:31 PM, Mafalda Viana wrote:
>>
>> Dear R users,
>>>
>>> I am trying to build a barplot2 graph however I can't find a way of
>>> defining
>>> the scale for the x-axis.
>>>
>>> I would like to show in my x-axis only the numbers 0, 25, 50, 75
>>> etc. (so
>>> far R is giving me a random scale hard to interpret and it doens't
>>> look
>>> nice...). Could anyone advise me on how to do this please, it
>>> would be a
>>> great help! Thank you.
>>>
>>> Below I show the code I have been using for my plots.
>>>
>>> my.plot<- barplot2(mydata.y, names.arg=mydata.x, width=1,
>>> xlab="km", ylab="kg", main="plot.name",
>>> plot.ci=TRUE, ci.l=data.lci,ci.u=data.uci, ci.col = "red", font=40,
>>> font.lab=60, xlim=c(0,250), xpd=FALSE)
>>>
>>
>>
>> It is not entirely clear what you are doing here an we cannot
>> reproduce it
>> lacking your data.
>>
>> Your x (horizontal) axis will have one tick mark below each bar,
>> using the
>> labels that you have indicated with mydata.x. Based upon your code,
>> the x
>> axis values would appear to be measures of distance.
>>
>> Your y (vertical) axis would appear to be a measure of weight,
>> perhaps
>> being some descriptive statistic (eg. mean) for weight at each
>> distance.
>>
>> It is unclear what you want to do with the x axis in lieu of the
>> default
>> values. Modifying the tick marks on the y axis would seem to make
>> more sense
>> here.
>>
>> In general, with any R graphic, you would use the arguments 'xaxt =
>> "n"'
>> and/or 'yaxt = "n"' to disable the default drawing of the x and y
>> axes,
>> respectively. You would then call the axis() function passing the
>> specific
>> locations and values of the tick marks that you wish to draw. See ?
>> par and
>> ?axis for more information on these.
>>
>> BTW if you are not plotting counts or proportions/percentages, then a
>> barplot is not the best approach for the display of summarized
>> continuous
>> data. I would use a point plot with error bars/confidence
>> intervals. See the
>> errbar() function in the Hmisc package or the plotCI() or plotmeans()
>> functions in gplots. You could also create something manually by
>> using
>> plot() and then either segments() or arrows() for the CIs.
>>
>> HTH,
>>
>> Marc Schwartz
>>
>>
>>
>
>
> --
> Mafalda Viana
> ______________________________________________
> 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.
More information about the R-help
mailing list