Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

R how to change shape/size of plots save as pngs

I am trying to change the size/shape of the base plots in R so they are larger and have closer to a rectangular shape than a square shape at default. Here is an example of one of my plots:

  png(file.path("plots",sets[set],"rspGREKM_vs_Star_Age.png"))
  plot(x=ages, y=growth.rates, main=paste(sets[set],"rspGREKM vs Star Age",sep=" "), pch=3, col="purple",
        xlab=expression("Star Age (days)"), ylab=expression("rspGREKM"))
  dev.off()

I tried including dev.new(width=5, height=4), but there was no noticeable change. What is the appropriate way to do this?

question from:https://stackoverflow.com/questions/65910580/r-how-to-change-shape-size-of-plots-save-as-pngs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The function png() has arguments for defining the width and height of the graphics device, as well as an extra argument for entering the desired units of those dimensions. Check args(png) or ?png.

here is a reproducible example, the first part will produce a rectangular plot, while the second part will produce the default square-shaped plot.

data("mtcars")
##rectangular plot
png("p1.png", width = 640, height = 420, units = "px")
plot(mtcars$mpg, type="l",  main = "p1" )
dev.off()
## default plot
png("p2.png")
plot(mtcars$mpg, type="l", main = "p2" )
dev.off()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...