R-3.1.1, Win7x64
Hi, I have data where two variables are measured such that X runs from 0 to 70 and Y runs from 0 to 100. I want to make a simple scatterplot of the observations.
The scatterplot should be dimensionalized, such that the x-axis (running from 0-70) is .7 the size of the y-axis (which runs from 0-100).
I use the following code
plot.new()
plot(0, 0, asp = 1, xlim = c(0, 70), ylim = c(0, 100), xlab = "", ylab = "", type = "n")
I am surprised to see that this yields a graph as follows:
Two things are not how I expected it: 1) the x-axis and y-axis are NOT restricted to their xlim and ylim values. (Why is that?) and 2) the figure is almost square.
I can resize the figure manually, by manually resizing the R window or the Rstudio window before using the code, but that is not feasible, because I have a great many figures to draw, many with varying xlim and ylim sizes and these figures need to be inserted into preformatted reports later (which is why they need to fulfill these exact layout demands).
I have also tried using
dev.new(width = 7, height = 10)
but that didn't help either.
My question is:
1) how can I "force" the figure to be restriced to the exact xlim and ylim ranges passed to the function?
and
2) how to generate a figure with the exact relative dimensions (the x-axis being .7 times as wide as the length of the y-axis)
Best Solution
The discussion of
asp
in the help forplot.window
implies thatasp
will overridexlim
andylim
settings (if you look at the help forplot
, it directs you toplot.window
to find out more aboutasp
):As @mr.joshuagordon noted, you can use the
pdf
(orpng
orjpeg
if you want bitmap output) function and play with the dimensions to get the aspect ratio you want, while removing theasp
argument fromplot
so that you can set thexlim
andylim
values.Another option is to switch to
ggplot2
, which makes it easy to set axis limits and aspect ratio separately:UPDATE: Here's how to control
xlim
,ylim
and the aspect ratio independently in base graphics: Instead ofasp
, use the graphical parameterpin
to set the physical dimensions of the plot region. This setting does not affect the nominal values ofxlim
andylim
, but will change the physical distance measure of 1 x-unit and 1 y-unit. Here are some examples:Example 1: We'll create two panels on one PDF page, each with a different aspect ratio:
Example 2: Showing that you'll get an error if you set
pin
to be larger than the size of the graphics device. We'll use the default device (RStudioGD
in my case).Example 3: The same error occurs if you exceed the size of your
pdf
(orpng
, etc.) device: