Steve Murray
2009-Jun-15 17:07 UTC
[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
Matthieu Dubois
2009-Jun-15 18:49 UTC
[R] Aligning axis values when plotting more than one graph on same axes
Dear Steve, it will be difficult to help you without the code you are using to produce the plots. Could you please post a complete example with some (possibly generated) data and your code ? Regards, Matthieu Matthieu Dubois Post-doctoral fellow Psychology and NeuroCognition Lab Grenoble, France
Greg Snow
2009-Jun-15 20:26 UTC
[R] Aligning axis values when plotting more than one graph on same axes
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.
Jim Lemon
2009-Jun-16 12:08 UTC
[R] Aligning axis values when plotting more than one graph on same axes
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