Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making square axes in R

Tags:

plot

r

graphics

You need to also set pty="s" in the graphics parameters to make the plot region square (independent of device size and limits):

par(pty="s")
plot(iris$Petal.Width, iris$Petal.Length, asp=1)
lines(2+c(0,1,1,0,0),3+c(0,0,1,1,0)) # confirm square visually

talllong


First of all, for me the plot already comes out square (big image). Clearly for you this is not the case, and you might need to make plots larger than the screen anyhow.

So, the size of the plot is controlled by the size of the output area, ie the plot window, the image file, or whatever else. Using Rstudio, you can use the built-in GUI the specify plot size. If you insist on using the base R console, you'll need to manually do the exporting. First open the file:

png("image.png", width=600, height=600)

This will open an image file in the working directory with equal proportions. Now plot:

x = iris$Petal.Width
y = iris$Petal.Length
all = c(x,y)
range = c(min(all), max(all))
plot(x, y, xlim=range, ylim=range)

And close the file:

dev.off()

The result:

enter image description here