# [R] adding percentage secondary y-axis

John Kane jrkrideau at yahoo.ca
Thu Nov 23 19:21:41 CET 2017

```
Actually Petr, I don't see that a secondary y-axis a problem here. If I understand it correctly Eliza is simply presenting the same data against two different scales. We in North America sometimes need to do the same thing when graphing, say  temperature data. We might want degrees Celsius on the y-axis and degrees Fahrenheit on the secondary y-axis because the USA still uses the antiquated Imperial measurement system. I don't think even Hadley would object here.
The deadly sin of a secondary y-axis is when there are two different variables are being graphed, say speed on the first axis and gasoline consumption on the other axis.  It creates a perceptual nightmare.
On Thursday, November 23, 2017, 12:28:36 PM EST, Eliza Botto <eliza_botto at outlook.com> wrote:

Thank you very much peter.

It worked out nicely.

I have additional question. How can I get Y-axis on log-scale?

Thank you very much in Advance,

Eliza

UoS
PP

________________________________
From: PIKAL Petr <petr.pikal at precheza.cz>
Sent: 23 November 2017 16:22:39
To: Eliza Botto; r-help at r-project.org
Subject: RE: adding percentage secondary y-axis

Hi

It is usually not recommended but if you insist

maybe
library(plotrix)
?twoord.plot
twoord.plot(lx=D[,1],ly=D[,2], rx=D[,1], ry=D[,3])

or

plot.yy(x=D[,1],yright=D[,3], yleft=D[,2])

which allows only one x axis (see below).

Cheers
Petr

plot.yy <- function (x, yright, yleft, yleftlim = NULL, yrightlim = NULL,
xlab = NULL, yylab = list(NA, NA), pch = c(1, 2),
col = c(1,2), linky = F, smooth = 0, lwds = 1, length = 10,
format = "%d/%m", rect = NULL, type = "p", ...)
{
par(mar = c(5, 4, 4, 2), oma = c(0, 0, 0, 3))
plot(x, yright, ylim = yrightlim, axes = F, ylab = "", xlab = xlab,
pch = pch[1], col = col[1], type = type, ...)
if (!is.null(rect))
rect(x[rect[1]], rect[2], x[rect[3]], rect[4], col = "grey")
points(x, yright, ylim = yrightlim, ylab = "", xlab = xlab,
pch = pch[1], col = col[1], ...)
axis(4, pretty(range(yright, na.rm = T), 10), col = col[1])
lines(x, yright, col = col[1], ...)
if (smooth != 0)
lines(supsmu(x, yright, span = smooth), col = col[1],
lwd = lwds, ...)
if (is.na(yylab[[1]]))
mtext(deparse(substitute(yright)), side = 4, outer = T,
line = 1, col = col[1], ...)
else mtext(yylab[[1]], side = 4, outer = T, line = 1, col = col[1],
...)
par(new = T)
plot(x, yleft, ylim = yleftlim, ylab = "", axes = F, xlab = xlab,
pch = pch[2], col = col[2], ...)
box()
axis(2, pretty(range(yleft, na.rm = T), 10), col = col[2],
col.axis = col[2])
if (!inherits(x, c("Date", "POSIXt")))
axis(1, pretty(range(x, na.rm = T), 10))
else {
if (inherits(x, "POSIXt")) {
l <- length(x)
axis(1, at = x[seq(1, l, length = length)],
labels = format(as.POSIXct(x[seq(1,l, length = length)]),
format = format))
}
else {
if (inherits(x, "Date")) {
l <- length(x)
axis(1, at = x[seq(1, l, length = length)],
labels = format(as.Date(x[seq(1,l, length = length)]),
format = format))
}
else {
print("Not suitable x axis")
}
}
}
if (is.na(yylab[[2]]))
mtext(deparse(substitute(yleft)), side = 2, line = 2,
col = col[2], ...)
else mtext(yylab[[2]], side = 2, line = 2, col = col[2],
...)
lines(x, yleft, col = col[2], lty = 2, ...)
if (smooth != 0)
lines(supsmu(x, yleft, span = smooth), col = col[2],
lty = 2, lwd = lwds, ...)
}

> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Eliza Botto
> Sent: Thursday, November 23, 2017 3:31 PM
> To: r-help at r-project.org
> Subject: [R] adding percentage secondary y-axis
>
> Dear useRs,
>
>
> I have this dataset (D) with three columns.
>
>
> > dput(D)
>
> structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
> 2.990484802, 3.005018792, 3.019552781, 3.03408677, 3.048620759,
> 3.063154749, 3.077688738, 3.092222727, 3.106756717, 3.121290706,
> 3.135824695, 3.150358684, 3.164892674, 3.179426663, 3.193960652,
> 3.208494642, 3.223028631, 3.23756262, 3.252096609, 3.266630599,
> 0.488381368, 0.976762736, 1.465144104, 1.953525472, 2.44190684,
> 2.930288208, 3.418669576, 3.907050944, 4.395432311, 4.883813679,
> 5.372195047, 5.860576415, 6.348957783, 6.837339151, 7.325720519,
> 7.814101887, 8.302483255, 8.790864623, 9.279245991, 9.767627359), .Dim =
> c(20L, 3L))
>
>
> The first column represents the index values while the second column contains
> the response values. The third column in actually the percentage increase in the
> response values by increasing a unit index value.
>
>
> I made a plot and obtained a single line by;
>
>
> >plot(D[,1],D[,2],type="l")
>
>
> The primary y-axis represents the usual response values. Now I want to add a
> secondary y-axis representing  the percentage value shown against response
> value. So that, when a reader sees the graph she could easily visualize not only
> the decimal response value but also the percentage increase against that
> response value from the secondary axis without plotting any extra line.
>
>
> I thank you in advance,
>
>
> Stay Blessed!!!!
>
>
>
> Eliza
>
>
> UoS
>
>      [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

________________________________
Tento e-mail a jak�koliv k n�mu p�ipojen� dokumenty jsou d�v�rn� a jsou ur�eny pouze jeho adres�t�m.
Jestli�e jste obdr�el(a) tento e-mail omylem, informujte laskav� neprodlen� jeho odes�latele. Obsah tohoto emailu i s p��lohami a jeho kopie vyma�te ze sv�ho syst�mu.
Nejste-li zam��len�m adres�tem tohoto emailu, nejste opr�vn�ni tento email jakkoliv u��vat, roz�i�ovat, kop�rovat �i zve�ej�ovat.
Odes�latel e-mailu neodpov�d� za eventu�ln� �kodu zp�sobenou modifikacemi �i zpo�d�n�m p�enosu e-mailu.

V p��pad�, �e je tento e-mail sou��st� obchodn�ho jedn�n�:
- vyhrazuje si odes�latel pr�vo ukon�it kdykoliv jedn�n� o uzav�en� smlouvy, a to z jak�hokoliv d�vodu i bez uveden� d�vodu.
- a obsahuje-li nab�dku, je adres�t opr�vn�n nab�dku bezodkladn� p�ijmout; Odes�latel tohoto e-mailu (nab�dky) vylu�uje p�ijet� nab�dky ze strany p��jemce s dodatkem �i odchylkou.
- trv� odes�latel na tom, �e p��slu�n� smlouva je uzav�ena teprve v�slovn�m dosa�en�m shody na v�ech jej�ch n�le�itostech.
- odes�latel tohoto emailu informuje, �e nen� opr�vn�n uzav�rat za spole�nost ��dn� smlouvy s v�jimkou p��pad�, kdy k tomu byl p�semn� zmocn�n nebo p�semn� pov��en a takov� pov��en� nebo pln� moc byly adres�tovi tohoto emailu p��padn� osob�, kterou adres�t zastupuje, p�edlo�eny nebo jejich existence je adres�tovi �i osob� j�m zastoupen� zn�m�.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.

[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.
[[alternative HTML version deleted]]

```