Remi.b
Remi.b

Reputation: 18239

Modifying aspect ratio of a plot in R

Here is my first plot

par(bg="white")
image(m, main = paste("generation: ",generation), ylab="", col=heat.colors(100), xaxt="n", yaxt="n", xlab="")

enter image description here

It is a square, and I'd like it to be a rectangle. So I did

par(bg="white", mar=c(16,1,16,1))
        image(m, main = paste("generation: ",generation), ylab="", col=heat.colors(100), xaxt="n", yaxt="n", xlab="")

enter image description here

But then the title (main) is really far away from the plot. So I did

par(bg="white", mar=c(16,1,16,1))
        image(m, ylab="", col=heat.colors(100), xaxt="n", yaxt="n", xlab="")
        legend(0.32, 3.5, paste("Generation: ", IntFormat(generation, 4)), border="white", xpd=TRUE, box.col="white", cex=1.5)

enter image description here

Well.. I wouldn't say it is splendid, but I was satisfied. So let's put that into a .png

png(paste0(folder.images, "pg_",IntFormat(generation,4),".png"))
        par(bg="white", mar=c(16,1,16,1))
        image(m, ylab="", col=heat.colors(100), xaxt="n", yaxt="n", xlab="")
        legend(0.32, 3.5, paste("Generation: ", IntFormat(generation, 4)), border="white", xpd=TRUE, box.col="white", cex=1.5)
        dev.off()

and here is what the .png looks like!

enter image description here


  1. Is there a better solution to make a rectangle out of my square than using the parameter mar that force me to add a title with legend and to search by trial and error where the center to the picture is for the title.

  2. Why is the .png different from what is shown in the R window?

Upvotes: 1

Views: 2246

Answers (1)

user3710546
user3710546

Reputation:

I could achieve this:

png("test.png", res = 150, width = 8, height = 1, units = "in")
par(mar = c(1,1,1,1))
image(matrix(1:10, ncol=1), ylab="", col=heat.colors(100), xaxt="n", 
      yaxt="n", xlab="", main="Generation: 0001")
dev.off()

enter image description here

Upvotes: 3

Related Questions