[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:58:05 CET 2019


Hm,

I just wonder why you do not use ggplot?

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)

library(ggplot2)
p <- ggplot(mydat, aes(x=datum, y=counts, colour=vzor))
p+geom_point(size=3)+geom_line()

The only problem is that it uses only one y axis but allows extensive annotation changing.

Even axes range manipulation is possible, although not recommended.
http://rpubs.com/kohske/dual_axis_in_ggplot2

Cheers
Petr

> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Ogbos Okike
> Sent: Wednesday, November 27, 2019 8:56 AM
> To: Rui Barradas <ruipbarradas using sapo.pt>
> Cc: r-help <r-help using r-project.org>
> Subject: Re: [R] Stacking two graphs with different x and y scale on the same
> graph
> 
> Dear Rui,
> Many thanks. It is indeed close to what I am looking for apart from the issues
> on the axes.
> 
> I am attaching the result plot. The minimum and maximum values of y1 are
> respectively -5.470919 and 1.298329 while they are 0.0002937659 and
> 4.808186 for y2.
> 
> New problems are: (1) I can't labelled the axis as (x1,x2) persist when I tried
> to label the axes. (2) The two plots are merging at some points. I only want
> them close but not merging as that does not allow for meaningful
> comparison.
> 
> It would be great if you could adjust the code further to minimize these
> problems.
> 
> Thank you.
> Best wishes
> Ogbos
> 
> On Wed, Nov 27, 2019 at 7:58 AM Rui Barradas <ruipbarradas using sapo.pt>
> wrote:
> >
> > 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.2983
> > > 29),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.
> > >


More information about the R-help mailing list