[R] adding percentage secondary y-axis

PIKAL Petr petr.pikal at precheza.cz
Fri Nov 24 08:03:57 CET 2017


Hi

Not sure about twoord.plot, try its help page.

I did not intend log Y axis in my function.
This
plot.yy(x=D[,1],yright=D[,3], yleft=D[,2], log="y")

gives you log right axis.

You have the code, you can modify it according to your desire.

Cheers
Petr

From: Eliza Botto [mailto:eliza_botto at outlook.com]
Sent: Thursday, November 23, 2017 6:28 PM
To: PIKAL Petr <petr.pikal at precheza.cz>; r-help at r-project.org
Subject: Re: adding percentage secondary y-axis

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<mailto:petr.pikal at precheza.cz>>
Sent: 23 November 2017 16:22:39
To: Eliza Botto; r-help at r-project.org<mailto: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])
    if (linky)
        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],
        ...)
    if (linky)
        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<mailto: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
>

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



More information about the R-help mailing list