corrplot margins in RStudio are too small - r

When I produce a graphic in R-Studio, the margins are too small that it cuts off the graphic in the plot area. The output below follows this tutorial, which uses the corrplot library to run Pearson Chi-square residuals. No matter how I adjust my plot area or zoom or export, the bottom is still cut off. How do I adjust this?

I get the image displayed just fine. Maybe you can try playing around with the margins - mar parameter that is also internal to corrplot()
Here is what I get with some margins:
corrplot(chisq$residuals, is.cor=FALSE, mar=c(5,2,2,1))
You can also try transposing the matrix so it becomes extended horizontally, but not sure if that fits your needs:
corrplot(t(chisq$residuals), is.cor=FALSE, mar=c(5,2,2,1))

Related

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.

Forest plot (metafor) with 90 effect sizes: how to make text readable and adjust row spacing

I am working on a forest plot with the metafor package. With the help of (http://www.metafor-project.org/doku.php/plots:forest_plot_with_subgroups) and several forum entries I wrote a code but unfortunately the letters are blurry. I tried making the plot bigger with windows () which did help a bit but still it is not ready for putting in a paper.
It looked like this:also blurry with zooming in
When I used only a subset of effect sizes for the subgroup analysis the forest plot the text was better readable but only until about 15 effect sizes). And because the row spacing is so narrow I can´t even think about putting summary polygons for subgroups in between.
This was the code I used for my data (90 effect sizes in multilevel model):
forest(Model1,
slab= data$Author, #labeled by author
# cex=0.75, #only made the text bigger but did not change the row distance, so the text overlapped
ylim=c(-1, 100),
header="Author(s) and Year")
There were a lot of extra arguments in examples of forest plots I saw online but I am not sure of their meaning in forest plot.
Does someone know how to solve this spacing problem or what arguments I need to add to the code e.g. to broaden the space between the rows? Is there a “guide” for working with forest?
Thank you very much in advance!
Blurriness has nothing to do with the forest() function - it's a matter of how you are saving the plot. You should use functions like png() with a high enough resolution. Or use pdf() where you can zoom in to your heart's content without ever seeing any blurriness.
As for a "guide", I would suggest reading the documentation first:
https://wviechtb.github.io/metafor/reference/forest.rma.html
https://wviechtb.github.io/metafor/reference/forest.default.html
Then you will start to understand what the arguments do.

Adjusting margins for R plots

I'm interesting in adjusting margins for R plots. I use R Studio on MacOS, run on a 2013 intel CPU Macbook pro.
Here is the data I used for generating the plot:
spins <- runif(50, min = 0, max = 50)
Here is the code I used to generate the plot:
hist(spins)
Here is the results of the console:
> hist(spins)
Error in plot.new() : figure margins too large
Here are my questions:
How do I find out the current margins of this specific plot?
How do I modify the margins for this specific plot?
Is there a method to modify the margins for plots in general (e..g., Can I use 1 set of code to find the margins of the object and then use a second set of code to modify the margins so it will display in the plots area)?
Is there code I can use to automatically adjust the margins of the plots so that the desired plot displays every time?
The par function can be used to see the size of current margins and to set margin sizes (plus a whole bunch of other things).
Running par(c("mar", "mai")) will report the current margins (starting at bottom and going clockwise) in lines of text (mar) and inches (mai).
You can then set the margins using code like: par(mar=c(2,2,2,0)) or par(mai=rep(0.4, 4)).
But the problem is probably that you have made the plots pane in RStudio too small in at least one dimension, so when the hist function tries to create the plot there is not enough room for the plot and margin information. Try dragging the appropriate dividers within RStudio to make the plot panel larger then try your code again. Another option is to run the command dev.new() which will open a new window for the plots (using the default for your OS) and by default send the plot to that new window instead of the plot pane in RStudio.

Corrplot visualization

I try to use R package - corrplot.
An example, i do this code:
M<-cor(mtcars)
corrplot(M, method="circle")
I get this image. It's ok, but if I want to look it inr my RStudio, i get this:
The image is shifted down, part of the characters are not visible. If there were more variables to correlate, it would be much worse. Maybe there are customizable parameters for image output and / or centering it?

Error in plot.new() : figure margins too large in R

I'm new to R but I've made numerous correlation plots with smaller data sets. However, when I try to plot a large dataset (2gb+), I can produce the plot just fine, but the legend doesn't show up. Any advice? or alternatives?
library(gplots)
r.cor <- cor(r)
layout(matrix(c(1,1,1,1,1,1,1,1,2,2), 5, 2, byrow = TRUE))
par(oma=c(5,7,1,1))
cx <- rev(colorpanel(25,"yellow","black","blue"))
leg <- seq(min(r.cor,na.rm=T),max(r.cor,na.rm=T),length=10)
image(r.cor,main="Correlation plot Normal/Tumor data",axes=F,col=cx)
axis(1, at=seq(0,1,length=ncol(r.cor)), labels=dimnames(r.cor)[[2]],
cex.axis=0.9,las=2)
axis(2,at=seq(0,1,length=ncol(r.cor)), labels=dimnames(r.cor)[[2]],
cex.axis=0.9,las=2)
image(as.matrix(leg),col=cx,axes=T)
Error in plot.new() : figure margins too large
tmp <- round(leg,2)
axis(1,at=seq(0,1,length=length(leg)), labels=tmp,cex.axis=1)
This error can occur in Rstudio simply because your "Plots" pane is just barely too small. Try zooming your "Files, Plots, Packages, Help, Viewer" and see if it helps!
The problem is that the small figure region 2 created by your layout() call is not sufficiently large enough to contain just the default margins, let alone a plot.
More generally, you get this error if the size of the plotting region on the device is not large enough to actually do any plotting. For the OP's case the issue was having too small a plotting device to contain all the subplots and their margins and leave a large enough plotting region to draw in.
RStudio users can encounter this error if the Plot tab is too small to leave enough room to contain the margins, plotting region etc. This is because the physical size of that pane is the size of the graphics device. These are not independent issues; the plot pane in RStudio is just another plotting device, like png(), pdf(), windows(), and X11().
Solutions include:
reducing the size of the margins; this might help especially if you are trying, as in the case of the OP, to draw several plots on the same device.
increasing the physical dimensions of the device, either in the call to the device (e.g. png(), pdf(), etc) or by resizing the window / pane containing the device
reducing the size of text on the plot as that can control the size of margins etc.
Reduce the size of the margins
Before the line causing the problem try:
par(mar = rep(2, 4))
then plot the second image
image(as.matrix(leg),col=cx,axes=T)
You'll need to play around with the size of the margins on the par() call I show to get this right.
Increase the size of the device
You may also need to increase the size of the actual device onto which you are plotting.
A final tip, save the par() defaults before changing them, so change your existing par() call to:
op <- par(oma=c(5,7,1,1))
then at the end of plotting do
par(op)
If you get this message in RStudio, clicking the 'broomstick' figure "Clear All Plots" in Plots tab and trying plot() again may work.
This sometimes happen in RStudio. In order to solve it you can attempt to plot to an external window (Windows-only):
windows() ## create window to plot your file
## ... your plotting code here ...
dev.off()
I got this error in R Studio, and was simply fixed by making the sidebar bigger by clicking and dragging on its edge from right to left.
Picture here: https://janac.medium.com/error-in-plot-new-figure-margins-too-large-in-r-214621b4b2af
Check if your object is a list or a vector. To do this, type is.list(yourobject). If this is true, try renaming it x<-unlist(yourobject). This will make it into a vector you can plot.
Just zoom this area if you use RStudio.
I found this error today. Initially, I was trying to output it to a .jpeg file with low width and height.
jpeg("method1_test.jpg", width=900, height=900, res=40)
Later I increased the width and height to:
jpeg("method1_test.jpg", width=1900, height=1900, res=40)
The error was not there. :)
You can also play with the resolution, if the resolution is high, you need more width and height.
I had this error when I was trying to plot high dimensional data. If that's what is going on with you, try multidimensional scaling: http://www.statmethods.net/advstats/mds.html
I struggled with this error for weeks (using RStudio). I tried moving the plot window bigger and smaller, but that did not consistently help. When I moved (dragged) the application to my bigger monitor, the problem disappeared! I was stunned... so many wasted hours... I knew my code was correct...
If margin is low, then it is always better to start with new plotting device:
dev.new()
# plot()
# save your plot
dev.off()
You will never get margin error, unless you plot something large which can not be accommodated.
RStudio Plots canvas is limiting the plot width and heights. However if you make your plot from Rmarkdown code chunk, it works without canvas field limitation because plotting area set according to the paper size.
For instance:
```{r}
#inside of code chunk in Rmarkdown
grid <- par(mfrow=c(4, 5))
plot(faithful, main="Faithful eruptions")
plot(large.islands, main="Islands", ylab="Area")
...
par(grid)
```
I found the same error today. I have tried the "Clear all Plots" button, but it was giving me the same error. Then this trick worked for me,
Try to increase the plot area by dragging. It will help you for sure.
I have just use the Clear all plots then again give the plot command and it was helpfull

Resources