0

I get the code for plotting with two y-axis from the following website:

https://www.r-bloggers.com/2015/04/r-single-plot-with-two-different-y-axes/

Here is my code after referencing the code in the link:

    par(mar = c(5,5,2,5))
    with(df, plot(Date, get(element), type="l", col="red3", ylab="beta_mkt"))
    par(new = T)
    with(df, plot(Date, NBER, type="l", axes=F, xlab=NA, ylab=NA, cex=1.2))
    axis(side = 4)
    mtext(side = 4, line = 3, "NBER")

This code prints out a plot, but I wanted to save this plot in a variable so I can reference it later. Anyone know how to achieve it?

Thanks.

Gin

Gin
  • 47
  • 5
  • It would be better doing the plot with `ggplot2`, would you be open to consider that option? – Duck Dec 05 '20 at 00:01
  • 1
    Also, what do you mean by "reference it later". Do you mean you want to make it an object so you have access to its internal elements later on, or do you want to save it so you have a file that has the graph in it? – DaveArmstrong Dec 05 '20 at 00:11
  • I think you are looking for https://stackoverflow.com/questions/29583849/save-a-plot-in-an-object – Ronak Shah Dec 05 '20 at 00:45
  • @Gin: like this https://stackoverflow.com/a/55204648/786542? – Tung Dec 05 '20 at 01:07
  • @DaveArmstrong By reference it later I mean to print it elsewhere in the code – Gin Dec 05 '20 at 01:13
  • @Duck I tried to use ggplot, but the result is not as good as using the method above. Maybe because I'm not too familiar with ggplot – Gin Dec 05 '20 at 01:14
  • @Duck If I'm not wrong, ggplot requires that there exists some sort of numerical relationship between two y-axis, which is not the case in my data. – Gin Dec 05 '20 at 01:26

0 Answers0