1

Old-school plots are the only way of plotting the exact nls model I need, so I plot a useful graph. It is, though, full of text and elements (like lines) on top since I wrote the code for each element separately. I need to be able to name my plot as one single graph/image/plot.

The code looks like this:

    plot(x~y,data=df, lwd=2, col = c(1,2,3))[Generation], pch=c(15,16,17) [Generation])

    axis(1,at=c(2,4, 6),cex.axis=1.6)
    axis(2,at=c(0, 5, 10, 15, 20), las=1,cex.axis=1.6)

    curve(vbT(x,Linf=coef(VB)),col="black",add=TRUE, lty=1)

    segments(4.5,10, 4.5,0, col=rgb(0,0,1,alpha=0.8), lwd=c(1))

    text(2, 10, "Group A", col=rgb(0.1,0.4,0.45, alpha=1), cex = 1, pos=1) 

It has more elements, but this gives the idea. It's just longer, so the number of lines doesn't change the problem.

I tried with all kind of parentheses and arrows:

    Plot1<- {My code... all the lines}

I tried plot.default and some other functions. I just can`t manage to name all of this as one "image" or plot. I am very used to ggplot now, but I must use basic r plots again. Because I am trying to graph together with some ggplots using the function ggarrange.

Thanks for your help!!!

Cris
  • 21
  • 3
  • 3
    Is this one plot, or multiple combined? Does `main = "my title"` not work in the first `plot()` statement? If you want multiple plots, maybe you want this... https://stackoverflow.com/questions/14660372/common-main-title-of-a-figure-panel-compiled-with-parmfrow Or by name do you mean assign to a variable? So then maybe this? https://stackoverflow.com/questions/29583849/save-a-plot-in-an-object –  Apr 02 '21 at 17:33
  • Hi @Adam, Thanks. Yes, there are multiple plots combined. But, I mean not naming the plot, like giving it a title. I mean to give a name to the plot and be able to call it after by only writing a name. For example: PlotA<- MY CODE. And calling after only for PlotA or maybe plot(PlotA). An equivalent to PlotB<- ggplot()+geom_point(), and using after PlotB. – Cris Apr 02 '21 at 17:42
  • 1
    I follow now. The second link I think is that? I personally have put things like that into a function, which is one of the answers I believe. –  Apr 02 '21 at 17:44
  • @Adam. Thank you so very much. It works! – Cris Apr 02 '21 at 18:05

1 Answers1

0

You could use recordPlot as explained here :

df  <-  data.frame(x = 1:10, y = rnorm(10))

# Draw your plot
plot(y~x,data=df)
lines(rnorm(10) , col=2)

# Save your plot
p <- recordPlot()

# Do stuff

# Reuse your plot
p 
Trusky
  • 483
  • 2
  • 13