[R] Aligning axis values when plotting more than one graph on same axes

Steve Murray smurray444 at hotmail.com
Mon Jun 15 19:07:46 CEST 2009


Dear R Users,

I am trying to plot a barchart with a line graph superimposed (using par(new=TRUE)). There are 12 bars and 12 corresponding points for the line graph. This is fine, except that I'm encountering two problems:

1) The position of the points (of the line graph) are not centred on the middle (horizontally) of each corresponding bar. In fact, whilst the first point is located on the left-most side of the first bar, subsequent points drift further towards the right of the bars, until the final point is at the right-most position on the final bar. I have used names.arg=substr(month.abb, 1, 1) to represent the first letter of each month for the barplot and xaxt="n" for the overplotting line graph. Is there a way of properly aligning the x-axis values so that the points are centred horizontally on the bars?


2) Similarly, for both plots, I have set ylim=c(10000, 85000) so that both y-axes are in proportion with one another. However, when I allow both graphs to plot their y-axis values, it becomes apparent that there is a slight offset again in where the axis labels are positioned. For example, 80000 is positioned notably higher up the y-axis for the barchart than for the line graph. (N.B. I have used xpd=FALSE for the barchart to prevent bars falling outside of the plot area). How do I ensure that the y-axis labels are correctly aligned so that they are common to both graphs?

Many thanks for your help,

Steve




More information about the R-help mailing list