# [R] barplot with second y-axis

Marc Schwartz mschwartz at medanalytics.com
Fri Jul 5 15:56:32 CEST 2002

```> -----Original Message-----
> From: owner-r-help at stat.math.ethz.ch
[mailto:owner-r-help at stat.math.ethz.ch] On
> Behalf Of Nicolas Schneider
> Sent: Friday, July 05, 2002 7:08 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] barplot with second y-axis
>
> Hi everyone
>
> I have 2 variables measured at 12 different locations. The variables
are
> measured on different scales.
>
> I am now trying to make a barplot with 12 "groups" of 2 bars. However,
the 2
> bars of each "group" should be related to different y-axes. Thus, what
I need to
> do is a barplot with 2 y-axis and then plot the 2 variables, one
referring to
> the left y-axis and the other one to the right x-axis. As a beginner
with R
> (second day!), this problem is still slightly too difficult. I hope
someone can
> help.
>
> Many thanks,
>
> Nicolas

One approach is to use an adjustment factor to your second set of data,
such that the measures are "normalized" to the range of the first set of
data, since the plot region coordinates will be set for only one range.

For example, if your first set of data is in the range of 0 to 5 and
your second set of data is in the range of 0 to 25, divide your second
set of data by 5.

Pass that new set of numbers to barplot() using the "beside = TRUE"
argument.

To get the second set of axis labels for the right hand side, use the
axis() function.  See ?axis.

Use the "side = 4" argument, which places the axis on the right hand
side of the plot. Also use the "at = " and "labels =" arguments to
define the positions for your scale and for the vector of labels that
you wish to have drawn, keeping in mind that you need to adjust your
labeling (not the "at = ") with the same adjustment factor that you used
above.

For example, with the numbers that I gave above use something like:

axis(side = 4, at = 0:5, labels = (0:5 * 5))

This will give you 6 tickmarks at actual y coordinates of 0, 1, 2, 3, 4
and 5, but with text labels of 0, 5, 10, 15, 20 and 25.

HTH.

Marc

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```