[R] probelm with xlab ylab and xaxp barplot

Abdoulaye SARR abdoulayesar at gmail.com
Mon Jul 25 15:06:38 CEST 2016


>> Hi Marc and Others,


I am still struggling to have my slab and ylab displayed on a bar plot. Marc did useful advise on playing with mar settings. I tried may combinations and can’t have it work.

I paste the code I am suing hoping guidance on solving this issue.




## extract works for all time steps
d1<-read.nc(gp)

d2<-read.nc(er)

d3<-read.nc(me)

d4<-read.nc(ne)

d5<-read.nc(ar)

d6<-read.nc(cc)

d7<-read.nc(mr)

d8<-read.nc(ic)

z1<-d1$spei
z2<-d2$spei
z3<-d3$spei
z4<-d4$spei
z5<-d5$spei
z6<-d6$spei
z7<-d7$spei
z8<-d8$spei
#par(oma=c(2,2,2,2))  # all sides have 3 lines of space  

par(mar=rep(2,4))
#par(mar=c(5.1, 4.1, 2.1, 2.1))
#par(mai=c(1.02,0.82,0.82,0.42))
op <- par(oma=c(1,2,3,5))
#op <- par(oma=c(6,5,0,0))
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"))

mtext("a")
barplot(z2,xlab="Years", ylab="spei",  ylim=c(-2,2), col=ifelse(z2>0,"green","brown"))
mtext("b")
barplot(z3, ylim=c(-2,2), xlab="Years", ylab="spei", col=ifelse(z3>0,"green","brown"))
mtext("c")
barplot(z4, xlab="Years", ylab="spei", ylim=c(-2,2), col=ifelse(z4>0,"green","brown"))
mtext("d")
barplot(z5, xlab="Years", ylab="spei", ylim=c(-2,2), col=ifelse(z5>0,"green","brown"))
mtext("e")
barplot(z6, xlab="Years", ylab="spei", ylim=c(-2,2), col=ifelse(z6>0,"green","brown"))
mtext("f")
barplot(z7,xlab="Years", ylab="spei",  ylim=c(-2,2), col=ifelse(z7>0,"green","brown"))
mtext("g")
barplot(z8,  ylim=c(-2,2), xlab="Years", ylab="spei", col=ifelse(z8>0,"green","brown"))
mtext("h")
par(op)

Another solution with ggplot2 or lattice also welcome.


Best regards,

asarr
>> 
>> 
>> On Wed, Jul 20, 2016 at 6:03 PM, Marc Schwartz <marc_schwartz at me.com> wrote:
>> 
>> > 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
>> 
>> 
>> <sample_code_sar.R><sample_bar_sar.pdf>
> 


	[[alternative HTML version deleted]]



More information about the R-help mailing list