This question already has answers here:
How to define fixed aspect-ratio for (base R) scatter-plot
(2 answers)
Closed 1 year ago.
I am not sure if this question is related to R or tikzDevice.
I simply use R and the command plot(1).
The resulting graphic behave like a vector graphic. It's aspect ratio depends on the ratio of the window it is displayed in.
Can I set the aspect ratio explicite while calling plot()?
xlim doesn't affect the graphic output itself - just the content (the numbers on the x-axis).
Maybe there is a way with tikzDevice to fix the aspect ratio?
The script I use with tikzDevice looks like this.
#!/usr/bin/R -f
# load the library
library(tikzDevice)
# the output file
tikz('plot.tex')
#
plot(1, xlab='X-Axis', ylab='Y-Axis')
dev.off()
#Moderators: Please add the tag 'tikzDevice'.
The help page ?plot includes the asp argument which is used to control aspect ratio (more details are on the ?plot.window page).
I don't know if the tikz device handles this differently (because the height and width of the plotting are may change after the plot is created), but the place to start is trying something like:
plot(1, asp=1)
and then make sure that the size of the plotting area is the same in the final document.
Edit
You may not see much change in the plot when you only plot 1 point, but try the following commands to see the effect of setting asp:
theta <- seq(0,2*pi, length=200)
plot(cos(theta),sin(theta))
plot(cos(theta),sin(theta), asp=1)
For regular R and Rstudio at the defaults (on my computer at least) there is a visible difference in the 2 plots. I have not tried this with a tikz device.
You might also try the squishplot function from the TeachingDemos package (I have never tried it with tikz devices, but in regular R it works as another way to set the aspect ratio):
library(TeachingDemos)
op <- squishplot(c(-1,1),c(-1,1), asp=1)
plot(cos(theta),sin(theta))
par(op)
I have a laptop with low screen resolution. As a consequence, when I give a plot command in R the Quartz plot object runs off the screen.
Is it possible to change set some option -- preferably in my .Rprofile -- that will reduce the size of the default plot output when using interactive R?
?quartz
quartz.options(width=4, height=4)
(The default values are 7 and 7 which can eaisly be obtained by typing just:
quartz.options()[c("width", "height")] # .)
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
I want to create a graphics of a function in R. The code is:
x <- seq(from=0, to=1, by=0.00001)
f <- function(x) ....
y <- f(x)
plot(x, y, xlab="x", ylab="f(x)", pch=16, cex=0.5)
min(y)
[1] 0.2291203
max(y)
[1] 0.7708797
When I save the graphics as bmp from RGui, it looks like here and this is fine. When I save it as eps and include in LaTeX with:
\begin{figure}[htbp]
\centering
\includegraphics[scale=0.4]{./images/f-probart.eps}
\end{figure}
it is skewed, as shown in the screen capture from here
What is wrong? I guess there is a problem with exporting from RGui in eps, as the resulted eps is also shown skewed in IrfanView. Hence I suspect that is not the LaTeX inclusion code that is wrong...
How could I create this graphics with a requested resolution, say 244 dpi? Is there another package/function that allows me to export eps with a specific resolution?
Thanks
I cannot reproduce your error, so I guess it's something specific to your system. If I save as eps and include it in latex (using the graphicx package), everything works completely fine. Keep in mind that if you used the postscript() function in R, you have to specify the width and height of your picture as well. I could be wrong, but I think it defaults to the default values of the graphics window in R (which could explain the dimensions of your eps pictures).
If you saved from the graphics window, it normally should take the current width and height of the graphics window. It does so on my R version, but maybe your options are set differently? check ps.options() and see if width and height have value 0. If that's not the case, that could be the problem.
On a side-note : You could use pdf instead. See ?pdf in R. It allows you to specify the width and height of the picture, and reproduces correctly in Latex. You should use pdftex for building the file then.
My experience is that using pdf graphics and pdftex is less trouble than passing through PS. In fact, in latex there is no need to pass through eps any more to come to a decent pdf. Another advantage of using pdftex is that you can easily combine all graphics formats in the same document. (For EPS you need the epstopdf package)
2) the dpi requirement is only useful for grid images, so not for eps and pdf which are vectorized. I'd use png, that's the best format for graphs. See the option res in the function png().
png("somefile.png",res=244)
plot(x, y, xlab="x", ylab="f(x)", pch=16, cex=0.5)
dev.off()
Alternatively, you could use the function bmp() for bitmap graphics in exactly the same way. Don't forget the dev.off() at the end.
I used the Cairo package; the code was:
Cairo(24000,24000,file="a.ps",type="ps",bg="transparent",pointsize=12, units="px", dpi=2400)
plot(x, y, xlab="x", ylab="f(x)", pch=16, cex=0.5, type='l')
dev.off()
The resulted graph looked fine. One question, however: according to #Joris Meys, the dpi is useless for vector graphics; in this case, why specifying dpi for the Cairo function is mandatory?