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

Greg Snow Greg.Snow at imail.org
Mon Jun 15 22:26:58 CEST 2009


Generally using par(new=T) is more complicated than it is worth, try a different method first.   One option is to use the updateusr function from the TeachingDemos package.  The first example on the help page shows adding a line to a barplot with everything aligned, follow that example as a start.  If that does not do everything you need, then send us a full reproducible example of what you have tried (it makes it easier for us to help you).

Also, creating a barplot with the lower y-limit at any value other than 0 is generally misleading.

Hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Steve Murray
> Sent: Monday, June 15, 2009 11:08 AM
> To: r-help at r-project.org
> Subject: [R] Aligning axis values when plotting more than one graph on
> same axes
> 
> 
> 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
> 
> ______________________________________________
> 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