[R] probelm with xlab ylab and xaxp barplot

Marc Schwartz marc_schwartz at me.com
Wed Jul 20 20:03:56 CEST 2016


> On Jul 20, 2016, at 4:00 AM, Abdoulaye SARR <abdoulayesar at gmail.com> wrote:
> 
> I have the color of my bar plot displayed correctly but don’t have xlab,  ylab  and xaxp don’t show up.
> 
> here is example of yearly data (25 years 1981-2005)
>> head(z1)
> [1] -0.1001726  0.2014272 -0.8556950  0.1920669 -0.8013520  1.3324949
> 
> code to display values
> 
> par(mar=rep(2,4))
> op <- par(oma=c(5,7,1,1))
> par(mfrow=c(4,2))
> 
> line = 3
> 
> barplot(z1, ylim=c(-2,2), xlab="Years", ylab="spei", xaxp=c(181,2005,1), col=ifelse(z1>0,"green","brown »))
> 
> hoe help on this issue
> 
> Fipou


Hi,

First, a general comment, which is that barplots are typically good for displaying counts and percentages, not continuous data points or perhaps estimates of means, etc. Your values for z1 above, suggest that you might be better off just plotting the points on the y axis against the years on the x axis. That is, for example:

  plot(1981:2005, z1, col = ifelse(z1 > 0, "green", "brown"), 
       ylab = "spei", xlab = "Years", pch = 19)

presuming that z1 has 25 values.

That being said, some additional notes to hopefully guide you here with barplot():

1. You appear to be wanting to plot a matrix of 8 plots in a 4 row by 2 column matrix. That is fine, but note that changing the graphic parameters associated with the spacing of margins, etc. in a matrix don't always provide a result similar to what you might find in a single plot. I would start by not adjusting par(mar) and par(oma) from their default values to get an idea of what the plot looks like with default settings and then modify from there so that you can see how any adjustments affect the result. You may be adjusting the margins for each plot and the outer margins of the overall matrix in a manner that conflicts.


2. In the case of a vertical barplot, the bars are not centered around integer values on the x axis, as they would be in say a boxplot. In the help for barplot() you will note that the Value section indicates that barplot returns a vector (by default) of the bar midpoints, which can then be used for annotation on the relevant axis. There are examples of the use of this on the barplot help page. Your values for 'xaxp' (which presumably has a typo for 1981, as 181) will not be correct here. Thus:

  MP <- barplot(z1, ...)

where 'MP' will contain the individual bar midpoints and then you can use code like:

  axis(1, at = MP, labels = 1981:2005, ...)

to place annotations below each bar. See ?axis as well as ?mtext for additional information on plot annotations.

Another option is to use the names.arg argument in barplot, to provide the names for each bar:

  barplot(z1, names.arg = 1981:2005, ...)

You will also likely have to adjust the font sizes for text spacing, as the defaults may be too large for all labels to display given the large number of bars. The cex* family of graphic parameters can be helpful. See the arguments in ?barplot and in ?par for more information.

Regards,

Marc Schwartz



More information about the R-help mailing list