[R] Stacking two graphs with different x and y scale on the same graph

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Wed Nov 27 09:10:29 CET 2019


Hi

In ancient times I developped function plot.yy which requires some data twisting but should do plotting with one x axis and two y axes and should estimete ranges automatically.

datA <- data.frame(datum=x1, counts=y1)
datA$vzor <- "A"
datB <- data.frame(datum=x2, counts=y2)
datB$vzor <- "B"
mydat <- merge(datA, datB, all=T, by="datum")

plot.yy(mydat$datum, mydat$counts.x, mydat$counts.y, col=c("red", "blue"), linky=T)


## The function is currently defined as
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 <r-help-bounces using r-project.org> On Behalf Of Rui Barradas
> Sent: Wednesday, November 27, 2019 7:59 AM
> To: Ogbos Okike <giftedlife2014 using gmail.com>; r-help <r-help using r-project.org>
> Subject: Re: [R] Stacking two graphs with different x and y scale on the same
> graph
> 
> Hello,
> 
> The following is not a complete solution, the axis ranges are wrong, but it
> gets you closer, I think.
> 
> 
> op <- par(mar = c(5, 5, 5, 5))
> 
> plot(c(x1, x2), c(y1, y2), type = "n",xaxt="n", yaxt="n",
>       ylim = range(c(y1, y2)))
> 
> par(new=TRUE)
> plot(x1,y1,pch=0,type="b",col="red",yaxt="n",
>       xlab = "", ylab="")
> axis(side=2, at=c(-5,0,2))
> mtext("red line", side = 2, line=2.5, at=0)
> 
> par(new=TRUE)
> plot(x2, y2, pch = 1,type="b",col="blue",
>       xaxt="n", yaxt="n",
>       xlab="", ylab="")
> axis(side=4, at=c(-5,-1,0), labels=c("98%","100%","102%"))
> mtext("blue line", side=4, line=2.5, at=0)
> 
> par(op)
> 
> 
> 
> Hope this helps,
> 
> Rui Barradas
> 
> Às 02:53 de 27/11/19, Ogbos Okike escreveu:
> > Dear Contributors,
> > I have two data. A is of the form:
> > 05 01 01  -0.00376058013285748
> > 05 01 02  -0.0765481943910918
> > 05 01 03  -1.28158758599964
> > 05 01 04  -1.51612545416506
> > 05 01 05  -1.39481276373467
> > 05 01 06  -1.17644992095997
> > 05 01 07  -0.788249311582716
> > 05 01 08  -0.925737027403825
> > 05 01 09  -1.02278717974814
> > 05 01 10  -0.982349616271341
> > 05 01 11  -0.61032403228481
> > 05 01 12  -0.197860884821482
> > 05 01 13  -0.173598346735404
> > 05 01 14  -0.270648499079717
> > 05 01 15  -0.173598346735404
> > 05 01 16  -0.343436113337951
> > 05 01 17  -0.949999565489903
> > 05 01 18  -3.60270372956778
> > 05 01 19  -5.47091916219579
> > 05 01 20  -4.67834291805057
> > 05 01 21  -5.42239408602363
> > 05 01 22  -4.19309215632901
> > 05 01 23  -1.79918839850264
> > 05 01 24  -1.04704971783422
> > 05 01 25  -0.642674083066247
> > 05 01 26  -0.505186367245138
> > 05 01 27  -0.472836316463701
> > 05 01 28  -0.537536418026576
> > 05 01 29  -0.311086062556513
> > 05 01 30  -0.00376058013285748
> > 05 01 31  -0.254473473688998
> > 05 02 01  -0.197860884821482
> > 05 02 02  -0.23021093560292
> > 05 02 03  -0.238298448298279
> > 05 02 04  -0.157423321344685
> > 05 02 05  -0.060373169000373
> > 05 02 06  0.109464597602174
> > 05 02 07  0.02858947064858
> > 05 02 08  -0.0846357070864511
> > 05 02 09  -0.189773372126123
> > 05 02 10  -0.278736011775076
> > 05 02 11  -0.302998549861154
> > 05 02 12  -0.0684606816957324
> > 05 02 13  0.0852020595160955
> > 05 02 14  0.133727135688252
> > 05 02 15  0.0528520087346581
> > 05 02 16  0.0771145468207362
> > 05 02 17  -0.0361106309142949
> > 05 02 18  -0.205948397516842
> > 05 02 19  -0.383873676814748
> > 05 02 20  -0.383873676814748
> > 05 02 21  -0.294911037165795
> > 05 02 22  -0.197860884821482
> > 05 02 23  -0.214035910212201
> > 05 02 24  -0.165510834040045
> > 05 02 25  -0.0522856563050137
> > 05 02 26  0.0366769833439393
> > 05 02 27  0.141814648383611
> > 05 02 28  0.101377084906814
> > 05 03 01  0.15798967377433
> > 05 03 02  0.222689775337205
> > 05 03 03  0.27930236420472
> > 05 03 04  0.327827440376876
> > 05 03 05  0.214602262641845
> > 05 03 06  0.133727135688252
> > 05 03 07  0.166077186469689
> >   and B is of the form:
> > 05 01 03  1.0401704890785
> > 05 01 04  1.1881431442713
> > 05 01 05  0.899433543239033
> > 05 01 06  0.495029973508058
> > 05 01 18  2.51141960034673
> > 05 01 19  4.80818567931821
> > 05 01 20  3.82649399122216
> > 05 01 21  4.75619054623929
> > 05 01 22  3.25702525028531
> > 05 01 23  0.328654748869008
> > 05 02 10  0.0689360507407491
> > 05 02 11  0.192369729879942
> > 05 02 15  0.0684297713902015
> > 05 02 16  0.100584435166215
> > 05 02 17  0.295302934161718
> > 05 02 18  0.552388788420635
> > 05 02 19  0.811732847306371
> > 05 02 20  0.843313045760178
> > 05 02 21  0.757193220375875
> > 05 02 22  0.65352100387166
> > 05 02 23  0.68252482652902
> > 05 02 24  0.624510062816789
> > 05 02 25  0.479854370620533
> > 05 02 26  0.359002279153697
> > 05 02 27  0.212459089641907
> > 05 02 28  0.240784160160447
> > 05 03 01  0.144583652487177
> > 05 03 02  0.0345028244394553
> > 05 03 21  0.023582430982633
> > 05 03 22  0.000293765928922767
> > 05 03 27  0.0440288222469235
> > 05 03 28  0.106263428254761
> > 05 03 29  0.291212461872628
> > 05 03 30  0.198305017329253
> > 05 03 31  0.186935599530143
> > 05 04 01  0.316471519561273
> > 05 04 02  0.266260602009615
> > 05 04 03  0.0456391152384458
> > 05 04 04  0.113939833419049
> > 05 04 05  0.140500137811164
> > 05 04 06  0.374670064516577
> > 05 04 07  0.295820206701906
> > 05 04 08  0.0833493810907385
> > 05 04 10  0.0253248646840757
> > 05 04 11  0.188773903020133
> > 05 04 12  0.206619775067284
> > 05 04 13  0.408503282817833
> > 05 04 14  0.344129922512134
> > 05 04 15  0.283273728250647
> > 05 04 16  0.155780334719261
> > 05 04 17  0.0815692243668445
> > B is extracted from A and I wish to plot A and B on the same x-axis
> > but different y axis on the same graph. Stacking very close helps to
> > illustrates the common rapid variations in A, represented in B.
> >
> > One of the codes I tried without success is:
> > data <- read.table("A", col.names = c("year", "month", "day", "counts"))
> >
> > new.century <- data$year < 50
> >
> > data$year <- ifelse(new.century, data$year + 2000, data$year + 1900)
> >
> > data$date <- as.Date(ISOdate(data$year, data$month, data$day))
> > x1 = data$date
> > y1=data$counts
> > #y1=scale(data$counts)
> >
> >
> > data <- read.table("B", col.names = c("year", "month", "day", "counts"))
> >
> > new.century <- data$year < 50
> >
> > data$year <- ifelse(new.century, data$year + 2000, data$year + 1900)
> >
> > data$date <- as.Date(ISOdate(data$year, data$month, data$day))
> > x2 = data$date
> > y2=data$counts
> >
> >
> > pdf("PLOT.pdf")
> >
> >   par(mar=c(5,5,5,5))
> >
> > plot(x1,y1,pch=0,type="b",col="red",yaxt="n",ylim=c(-
> 5.470919,1.298329),ylab="")
> > axis(side=2, at=c(-6,0,2))
> > mtext("red line", side = 2, line=2.5, at=0)
> >
> > par(new=TRUE)
> > plot(x2,y2,pch=1,type="b",col="blue",yaxt="n",ylim=c(-4.808186,0.0),
> ylab="")
> > axis(side=4, at=c(-5,-1,0), labels=c("98%","100%","102%"))
> > mtext("blue line", side=4, line=2.5, at=100)
> > dev.off()
> >
> > Your assistance is ever appreciated.
> > Best wishes
> > Ogbos
> >
> > ______________________________________________
> > R-help using 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.
> >
> 
> ______________________________________________
> R-help using 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.


More information about the R-help mailing list