How can I create a new on-screen R plot window with a particular width and height (in pixels, etc.)?
4 Answers
Use dev.new()
. (See this related question.)
plot(1:10)
dev.new(width=5, height=4)
plot(1:20)
To be more specific which units are used:
dev.new(width=5, height=4, unit="in")
plot(1:20)
dev.new(width = 550, height = 330, unit = "px")
plot(1:15)
edit additional argument for Rstudio (May 2020), (thanks user Soren Havelund Welling)
For Rstudio, add dev.new(width=5,height=4,noRStudioGD = TRUE)
-
2I think the units are something other than pixels. I tried this example and it froze my system for 5 mins. The resulting plot window was huge as was everything displayed in it. It might be either inches or something ?? – Ryan R. Rosario Jan 25 '10 at 03:12
-
I would suggest looking at `?Devices`, because this will vary depending on which device you use. But yes, I think that it does default to inches. – Shane Jan 25 '10 at 03:19
-
6Units are in inches for onscreen display (e.g. windows or x11), and vector drawing devices (e.g. pdf, postscript), and in pixels for bitmap drawing devices (e.g. png, jpeg). Mointors usually display 72 or 96 pixels per inch, printing to paper varies from 150 to 1200 pixels per inch. – Richie Cotton Jan 25 '10 at 14:28
-
Thanks for that explanation. I wonder why there's an inconsistency there? It would be more obvious if everything was in pixels. – Shane Jan 25 '10 at 14:52
-
8if no result in Rstudio try: dev.new(width=5,height=4,noRStudioGD = TRUE) – Soren Havelund Welling Dec 08 '15 at 17:00
-
I love you. I've been looking for this solution all day. – Boromir Jan 26 '20 at 04:24
-
Is there any reason why this needs a call to `dev.new` rather than `par`? `par`'s `din`, `fin`, and `pin` arguments look relevant. – J. Mini Jul 10 '21 at 12:16
This will depend on the device you're using. If you're using a pdf device, you can do this:
pdf( "mygraph.pdf", width = 11, height = 8 )
plot( x, y )
You can then divide up the space in the pdf using the mfrow parameter like this:
par( mfrow = c(2,2) )
That makes a pdf with four panels available for plotting. Unfortunately, some of the devices take different units than others. For example, I think that X11 uses pixels, while I'm certain that pdf uses inches. If you'd just like to create several devices and plot different things to them, you can use dev.new(), dev.list(), and dev.next().
Other devices that might be useful include:
There's a list of all of the devices here.

- 46,512
- 18
- 65
- 82
A convenient function for saving plots is ggsave()
, which can automatically guess the device type based on the file extension, and smooths over differences between devices. You save with a certain size and units like this:
ggsave("mtcars.png", width = 20, height = 20, units = "cm")
In R markdown, figure size can be specified by chunk:
```{r, fig.width=6, fig.height=4}
plot(1:5)
```

- 9,525
- 5
- 58
- 102
As the accepted solution of @Shane is not supported in RStudio (see here) as of now (Sep 2015), I would like to add an advice to @James Thompson answer regarding workflow:
If you use SumatraPDF as viewer you do not need to close the PDF file before making changes to it. Sumatra does not put a opened file in read-only and thus does not prevent it from being overwritten. Therefore, once you opened your PDF file with Sumatra, changes out of RStudio (or any other R IDE) are immediately displayed in Sumatra.

- 1,339
- 1
- 15
- 26
-
1did work for me either in Rstudio until noRStudioGD was set to TRUE, dev.new(width=5,noRStudioGD = TRUE) – Soren Havelund Welling Dec 08 '15 at 16:58
-