0

How do I go about making the width of a graph narrower by adjusting the margins of the plotting device?

For example with:

plot(1:40)

Is there a parameter I can use to control the margins?

joran
  • 169,992
  • 32
  • 429
  • 468
user2846211
  • 949
  • 6
  • 16
  • 24
  • Since `graph` refers to a specific mathematical construct, in this case it's more appropriate to say `plot` or `figure`. – Superbest Feb 12 '14 at 12:30
  • @Spacedman is correct. The plot will fill the open device. If you're working in an IDE, simply resize the plot window. Otherwise use `windows()` or `x11()` – Andrie Feb 12 '14 at 12:33
  • 1
    See also http://stackoverflow.com/questions/1279003/specify-width-and-height-of-plot – Superbest Feb 12 '14 at 12:43
  • @Spacedman Only if the OP is good at converting pixels on screen to an absolute size of plot. Even the suggestion to control the size of the *device* won't control the size of the region where data gets plotted, if that is what the OP mean by "width". There could be real nuance to this question so I am only hovering on the **-1** vote at the moment hoping that they come back and clarify what they mean by "width". – Gavin Simpson Feb 12 '14 at 16:11

3 Answers3

1

par is used to set the graphical parameters.

mar gives the number of lines of margin to be specified on the four sides of the plot. A numerical vector of the form c(bottom, left, top, right).

To make the plot narrower:

par(mar=c(5,10,5,10))
plot(1:40)
TWL
  • 2,290
  • 1
  • 17
  • 21
1

In addition to the answers given, other plotting devices, like png() and pdf() have width and height options.

png(filename=..., width=200, heigth=800, units="px")
plot(1:40)
dev.off()
celiomsj
  • 329
  • 1
  • 5
  • The visual devices (`x11()`, `windows()`, and `quartz()`, as well as the generic `dev.new()`) also take the parameters of `height` and `width`. – Brian Diggs Feb 12 '14 at 15:02
0

As your question is almost opaque as to what you mean by "width" - the width of the device, the width of the region containing the data points, etc? - I'll put out another option, parameter pin:

layout(1:2)
plot(1:10)
op <- par(pin = c(3,2)) ## 3" by 2" high
plot(1:10)
par(op)
layout(1)

which produces

enter image description here

when used with

png("par-pin-plot.png", units = "in", height = 8, width = 5, res = 100)
...plot code...
dev.off()

If you then use parameter mai to set the margin sizes in inches as well, you can control the exact width of the plot region, margins, and overall figure.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453