Cleared all plots, now ggplot2 will not plot - r

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.

Related

par() function acting up in Rstudio - plotting weird results or no results at all

I've found a couple of similar posts but unfortunately not one reply seems to fix my problem.
Data = working on the uscrime dataset.
Essentially, I'm having trouble my plots when using par() in RStudio. It's not the first time it happens that all of the sudden after re-rerunning a chunk in R, the plot is no longer displayed inline nor in the Plots window.
My code is the following:
par(mfrow=c(3,5))
for(i in colnames(crime[,1:15])){
plot(crime[,i],crime[,16], main=print(paste("Crime vs", i)), xlab=i, ylab="Crime")
lines(lowess(crime[,i],crime[,16]), col="red")
abline(lm(crime[,16]~crime[,i]), col="blue")
}
The first time I ran the chunk the plots showed up:
Now, as soon as I tried to plot something different (same data but with a transformed column) using the same approach, nothing appeared inline. Here's my code:
par(mfrow=c(3,5))
for(i in colnames(crime2[,1:15])){
plot(crime2[,i],crime2[,16], main=print(paste("Crime vs", i)), xlab=i, ylab="Crime")
lines(lowess(crime2[,i],crime2[,16]), col="red")
abline(lm(crime2[,16]~crime2[,i]), col="blue")
}
I tried restarting R and running everything again and ow I'm getting a weird result where some of the information I passed to my first par() call is getting displayed in my charts (see how I have two blue curves when I should have a blue and a red one).
Now, I've been reading about 'devices' and plots but since I'm an R noob I can't figure out what's wrong on my own. It looks like I have to call dev.off() at some point but I don't quite get it. My expectation is that there's something wrong (or an additional step to take) with my par() calls but I haven't found the replies in R Documentation.
Here's a view of my Markdown config

R, plot keeps appearing in bottom left corner instead of centered

For some reason, when using ordihull and orditop to create a plot for my NMDS plot, I cannot for the life of me get it to be centered. I am using RStudio.
I have tried to reset the plot area using dev.off(), followed by plot.new() and using default values such as par(mfrow=c(1,1) par(mar=c(0,0,0,0)).
But here is an image of what my plot looks like using plot.new(), par(mar=c(10,10,10,10))
No matter what I put in par(mar), it is always cutting off my plot. I have no idea why this is happening or how to fix it.
Seems like when using ggplot2 (which is one the libraries I did load), the plot works by first running an empty plot such as plot(example_NMDS) without any other features, or running plot(example_NMDS, type="n") to set up the empty space first. Then run ordiplot. I think ordiplot(type="none") will also do the trick.

Labels cannot be placed in second boxplots in R?

If you run the following simple code, you will notice that although in the first boxplot there are labels, this is not the case with the second boxplot where there are no labels. How to fix this? Is it an internal R bug?
I have tried to store boxplots in a working directory just in case this is an R studio graphics problem, but still... the same issue arised even then so this is not an Rstudio problem.
df=data.frame("A"=rnorm(10),"B"=rnorm(10),"C"=rnorm(10),"D"=rnorm(10))
for (i in 1:2){
boxplot(df[,1:2],xaxt='n')
axis(1,at=c(1,2))
boxplot(df[,3:4],xaxt='n')
axis(1,at=c(3,4))}

Prevent multiple plots in RStudio's plot pane

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.

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.

Resources