Prevent multiple plots in RStudio's plot pane - r

I've currently got a function that loops, and with each loop, it plots some lines and the plot appears in RStudio's plot pane. What I've noticed is that every time the function loops and plots, it creates a brand new plot in the plot pane, so when the function has finished there's a multitude of plots stacked on top of each other in RStudio's plot pane. Is there any way to stop the build up of plots within RStudio? I still want the function to plot a new thing every loop (psuedo-animation), but I want to avoid the build up of plots.
I've tried calling dev.off() and dev.new() but this creates new windows that displays the plot, and I want to avoid this and keep the plot in RStudio's plot pane.
Thanks!

If your function outputs a plot per loop, this is the expected behaviour within RStudio. Some solutions you might consider:
assign each plot to a list element instead of outputting it, then deal with the list later
output each plot to a file instead of to the RStudio pane
if you really want animation (adding items to the same plot), look at gganimate

Try this
par(mfrow=c(1,2))
Let me know if it was you need.

Related

Cleared all plots, now ggplot2 will not plot

I've been having issues with ggplot2 for a few days. First, when I would try to run different codes, the p value from my previous plot would show up on the new one. This would happen even when I knew they should have different p values and the dots from the plots were visibly different. I then tried to clear all my plots using the "Clear all plots" broom button. Since I've done this, I have not been able to plot anything. The plots don't automatically pull up like they used to and when I call the plot, it doesn't come up because it appears that the plot isn't saved to my directory and isn't a part of my environment. I've tried running older codes that I know work and still haven't been able to produce any plots.
Try running
dev.off()
and replotting your graph.

Difference in plot when shown in R Studio and exported with ggsave()

I want to add an additional tick to my plot in ggplot2, and used Solution 2 as described in the post Annotate ggplot with an extra tick and label.
It worked fine for me, giving the following result in R Studio:
But when I try to save the result using ggsave() to create the a .pdf, .ps, or .png file, the red number is cut off half like this:
I have the feeling that the inner plot is printed first and later the margins are plotted on top of this.
Anybody has a hint?
Thank you Z. Lin! I just had a grid.draw(g) instead of g <- grid.draw(g). This dot in R always activates my python brain region :)

Multiple R plots with tooltips in one figure?

I want to create a new figure with interactive R-plots (with tooltips).
To show how I wish to have my plots I give an example using the mtcars data. I am using the package scatterD3 to get tooltips:
library(scatterD3)
data(mtcars)
tooltips <- paste('This is an incredible <strong>',rownames(mtcars),'</strong><br />with',mtcars$cyl,'cylinders !')
scatterD3(x=mtcars$wt,y=mtcars$mpg,tooltip_text=tooltips)
Now I don't want only one plot of this type in one figure. Usually it is possible to use par(mfrow=(i,j)) to create a figure with more than one plot. But it seems not to work for my interactive plots. Is there a way to do this?
Have you given a look to the plotly R package?
It has also a function to render the ggplots in ggplotly
An idea could be to create a multiplot with ggplot2 and than render it with ggplotly.
I've not yet tried it, so I don't know if it's really possible.

How do you delete the current (but not all) plots in the RStudio plotting device?

How do you delete the current (but not all) plots in the RStudio plotting device?
dev.off() will remove all plots, but what if I just want to remove one? I don't want to have to press that red 'x' button because I want to remove one plot without pressing a button.
In R, you would just use dev.new() before each plot, so you dev.off() to only clear the last plot.
In RStudio, you can use x11(), windows() or quartz() (depending on your device) before each plot. Then call dev.off() to clear last plot. You can also use dev.set() to choose specific plots that way.
If your question is specifically asking to delete the last plot within the same RStudio window (rather than making new windows), not sure if it's possible, since RStudio treats that window as one device. An idea would be to look at a way to call the C++ function removePlot() in the RStudio project.
I found in the Github repository for RStudio the C++ code:
display.removePlot(display.activePlotIndex());
You could output the plots and manage the files that way.

Repeat plot command with minor changes in R

I made a plot in R and I want to repeat all the commands (like plot(), legend() or line()) that were carried out for this plot, with some minor changes. For example I want to set the axes to logarithmic scale and change the title of the plot.
In gnuplot I would use the replot command.
plot ...
set title "The same plot with logarithmic axes"
set logscale
replot
Is something like this possible in R. The only thing that comes to my mind of doing this (besides changing the values manually and re-run the lines of codes) would be setting up a function, that asks for all parameters that might be changed by the user.
Thanks for your help,
Sven
R uses a pen and paper graphics model - once the plot has been drawn on the device that is it. If you want to change some aspect of the plot, you need to replay the graphics function calls that produce the plot with the changes made to the code.
Depending on what you are really doing there are two options:
If this is just for you, write code in a text editor / IDE that knows R and can send chunks of code at a time to R. That way the code to produce the figure is recorded in a separate script which you can paste into/send to R making the changes you need each time to the script.
If you are going to be doing this often, then write yourself a wrapper plotting function that encapsulates the plot code you want but allows you to pass in arguments to alter the aspects you want.
Lattice and ggplot2 are a little different as they are based on grid graphics and create objects that when printed produce a plot on the device. One can manipulate that object to alter what is drawn, and with grid one can push and pop things on to / off a viewport.

Resources