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

Jim Lemon jim at bitwrit.com.au
Tue Jun 16 14:08:43 CEST 2009


Steve Murray wrote:
> 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?
>   
Hi Steve,
Have a look at the barp and twoord.plot functions in the plotrix package.

Jim




More information about the R-help mailing list