Home > Software design >  Adjusting Plot aspect ratio in R
Adjusting Plot aspect ratio in R

Time:04-04

Trying to change the aspect ratio of this plot so that its twice as long as it is tall, here is the code

plot(X,vw,
ylab= "Stress (MPa)", 
xlab= "Strain (mm)")
title("Veronda-Westmann")
lines(X,s,col="red")
legend(x=0, y=17, c("Veronda-Westmann", "Experimental"),cex=.8,col=c("black","red"),pch=c(1,NA),lty=c(NA,1))

enter image description here

I used code to try and specify height and width, but this didtn appear to work. New to R so really sorry if this is a stupid Q

CodePudding user response:

Are you looking to export this figure? If so, you can simply specify this on export:

x <- seq(0,5, 0.1)
y <- seq(0,15, 0.3)

Plot 1: native aspect ratio

png("test.png") # or pdf, etc
plot(x, y)
dev.off()

enter image description here

Plot 2: twice as wide:

png("test2.png", width = 1000, height = 500) # random dimensions
plot(x, y)
dev.off()

enter image description here

CodePudding user response:

Without reproducible data it is a bit hard to exactly reproduce your plot, but you can set asp to your plot which means you can modify the width of the x-axis in any ratio to the y-axis you want. You can use the following code:

X <- c(0,1,2,3,4,5, 6, 7, 8, 9, 10)
vw <- c(0, 0.5, 0.75, 1, 1.5, 3, 5, 8, 10, 15, 22)
s <- c(0, 0.5, 0.75, 1, 1.5, 3, 5, 8, 10, 15, 22)

plot(X,vw,
     ylab= "Stress (MPa)", 
     xlab= "Strain (mm)", asp = 2)
title("Veronda-Westmann")
lines(X,s,col="red")
legend("topleft", 
       c("Veronda-Westmann", "Experimental"),
       cex=.8,col=c("black","red"),
       pch=c(1,NA),
       lty=c(NA,1))

Output:

enter image description here

  • Related