# [R] adding percentage secondary y-axis

Eliza Botto eliza_botto at outlook.com
Thu Nov 23 18:28:22 CET 2017

```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
> 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]]

```