Aron_t
Aron_t

Reputation: 13

R double Y axis seperating

I was wondering if it is possible to seperate two plots from eachother (both should be on the same plot, using double Y axis). So the double plot should be split into two but without actually plotting them seperate - par(mfrow(1,2)).

Like this

I was trying to imitate it with layout plot, or with latticeExtra, ggplot but no success.

I have two different dataset one for the exchange rate one for the logaritmic returns.

par(mar=c(4,4,3,4))
plot(rates$EURHUF~rates$Date, type="l", ylab="Rate", main="EUR/HUF", xlab="Time")
par(new=TRUE)
plot(reteslog$EURHUF~rateslog$Date, type="l", xaxt="n", yaxt="n", ylab="", xlab="", col="red")
axis(side=4)
mtext("Log return", side=4, line=3)
legend("topleft", c("EUR/HUF Rates","EUR/HUF Logreturns"), col=c("black", "red"), lty=c(1,1))

Output of the code So far I am here, I just don't know how to seperate them or scale them (maybe using margin, or layout?)

Thank you very much guys for helping

Upvotes: 1

Views: 89

Answers (1)

Nick
Nick

Reputation: 349

I have a solution to this that isn't too outlandish, and is entirely in base, which is nice. For it to work, you just need to be able to force all of your data onto the same scale, which usually isn't a hassle.

The idea is that once your data is on the same scale, you can plot it all normally, and then add in custom axes that show the respective scales of the different data.

set.seed(1986)
d01 <- sample(x = 1:20,
              size = 200,
              replace = TRUE)

d02 <- sample(x = 31:45,
              size = 200,
              replace = TRUE)

# pdf(file = "<some/path/to/image.pdf>",
#     width = 4L,
#     height = 4L) # plot to a pdf
jpeg(file = "<some/path/to/image.jpeg>") # plot to a jpeg
par(mar=c(3.5, 3.5, 2, 3.5)) # parameters to make things prettier
par(mgp=c(2.2, 1, 0)) # parameters to make things prettier
plot(x = 0,
     y = 0,
     type = "n",
     xlim = c(1, 200),
     ylim = c(1, 50),
     xlab = "Label 01!",
     ylab = "Label 02!",
     axes = FALSE,
     frame.plot = TRUE)
points(d01,
       pch = 1,
       col = "blue") # data 01
points(d02,
       pch = 2,
       col = "red") # data 02
mtext("Label 03!",
      side = 4,
      line = 2) # your extra y axis label
xticks <- seq(from = 0,
              to = 200,
              by = 50) # tick mark labels
xtickpositions <- seq(from = 0,
                      to = 200,
                      by = 50) # tick mark positions on the x axis
axis(side = 1,
     at = xtickpositions,
     labels = xticks,
     col.axis="black",
     las = 2,
     lwd = 0,
     lwd.ticks = 1,
     tck = -0.025) # add your tick marks
y01ticks <- seq(from = 0,
              to = 1,
              by = 0.1) # tick mark labels
y01tickpositions <- seq(from = 0,
                        to = 50,
                        by = 5) # tick mark positions on the y01 axis
axis(side = 2,
     at = y01tickpositions,
     labels = y01ticks,
     las = 2,
     lwd = 0,
     lwd.ticks = 1,
     tck = -0.025) # add your tick marks
y02ticks <- seq(from = 0,
               to = 50,
               by = 5L) # tick mark labels
y02tickpositions <- seq(from = 0,
                        to = 50,
                        by = 5) # tick mark positions on the y02 axis
axis(side = 4,
     at = y02tickpositions,
     labels = y02ticks,
     las = 2,
     lwd = 0,
     lwd.ticks = 1,
     tck = -0.025) # add your tick marks
dev.off() # close plotting device

A pllot with two y axes

A few notes:

  1. Sizing for this plot was originally set for a pdf, which unfortunately cannot be uploaded here, however that device call is included as commented out code above. You can always play with parameters to find out what works best for you.
  2. It can be advantageous to plot all of your axis labels with mtext().
  3. Including simple example data in your original post is often much more helpful than the exact data you're working with. As of me writing this, I don't really know what your data looks like because I don't have access to those objects.

Upvotes: 1

Related Questions