Specify Width and Height of Plot
Asked Answered
N

4

25

I have a panel containing three plots. How can I use par to specify the width and height of the main panel so it is always at a fixed size?

Necessarian answered 14/8, 2009 at 17:6 Comment(0)
C
15

I usually set this at the start of my session with windows.options:

windows.options(width=10, height=10)

# plot away
plot(...)

If you need to reset to "factory settings":

dev.off()
windows.options(reset=TRUE)

# more plotting
plot(...)
Conduplicate answered 14/8, 2009 at 17:34 Comment(2)
This is Windows specific, I think.Rothschild
Does not work on Mac OSX: Browse[1]> windows.options(width=10, height=10) Error in windows.options(width = 10, height = 10) : could not find function "windows.options"Racism
V
34

You do that in the device, e.g.

x11(width=4, height=6)

and similarly for the file-based ones

pdf("/tmp/foo.pdf", width=4, height=6)

You can read the physical size via par("cin") etc but not set it.

Vaccine answered 14/8, 2009 at 17:10 Comment(2)
For completeness, width and height are specified in inch.Enosis
As @Enosis mentioned, but also if you specify unit='in', you must also specify res='xx' where xx is 72, for example.Swifter
S
18

Neither solution works in Jupyter notebooks. Here is a general approach that works in any environment:

options(repr.plot.width=6, repr.plot.height=4)

Just keep the following function handy:

set_plot_dimensions <- function(width_choice, height_choice) {
        options(repr.plot.width=width_choice, repr.plot.height=height_choice)
        }

EXAMPLE

Data

 x <-  c(37.50,46.79,48.30,46.04,43.40,39.25,38.49,49.51,40.38,36.98,40.00,38.49,37.74,47.92,44.53,44.91,44.91,40.00,41.51,47.92,36.98,43.40)

Call function with dimensions, and draw plot:

set_plot_dimensions(6, 4)
show_distribution(x, 'test vector')

enter image description here

set_plot_dimensions(16, 4)
show_distribution(x, 'test vector')

enter image description here

Swanky answered 20/5, 2018 at 22:50 Comment(0)
C
15

I usually set this at the start of my session with windows.options:

windows.options(width=10, height=10)

# plot away
plot(...)

If you need to reset to "factory settings":

dev.off()
windows.options(reset=TRUE)

# more plotting
plot(...)
Conduplicate answered 14/8, 2009 at 17:34 Comment(2)
This is Windows specific, I think.Rothschild
Does not work on Mac OSX: Browse[1]> windows.options(width=10, height=10) Error in windows.options(width = 10, height = 10) : could not find function "windows.options"Racism
U
0

I think a ggplot based answer is needed here...

If you want to save a figure as you have seen in the preview window, this would be the best practice:

  1. ggplot first
 ggplot(mtcars, aes(x=wt, y=mpg)) +
           geom_point(size=2, shape=23)
  1. Check the figure in Rstudio Plots window, if not satisfied, click Zoom, drag the pop-out window to your preferred size

  2. right click the figure and choose Inspect element

then you see the line

<img id="plot" width="100%" height="100%" src="plot_zoom_png?width=214&amp;height=151">

The above line suggests that your best width is 2.14 and best height at 1.51

  1. ggsave() what you have just seen
ggsave(filename = "foo3.png",width = 2.14, height = 1.51, dpi = 300)
# set the dpi as journal requirement, now the dpi only changes image quality, not image content, default unit is 'in' inch

If you want to shape your figure to fit a journal's need, this would be the best practice:

  1. Obtain parameters from the journal, for example width = 180mm, height =185mm following Nature Link

  2. Create a new Figure window using that parameters

# you only need noRStudioGD=TRUE if using Rstudio
dev.new(width = 90, height = 180, unit = "mm",noRStudioGD=TRUE)
#note that a new window will appear in your task bar
  1. plot() or ggplot() and preview the figure in that window

  2. ggsave() what you have seen

ggsave(filename = "foo3.png",width = 90, height = 180, unit = "mm", dpi = 300)
Uzia answered 20/2 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.