【问题标题】:Modifying aspect ratio of a plot in R修改R中绘图的纵横比
【发布时间】:2023-04-03 20:13:01
【问题描述】:

这是我的第一个情节

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

它是一个正方形,我希望它是一个矩形。所以我做到了

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="")

但后来标题(main)离剧情真的很远。所以我做到了

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)

嗯..我不会说它很棒,但我很满意。所以让我们把它放到一个.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()

这就是 .png 的样子!


  1. 有没有比使用参数mar 更好的解决方案来制作一个矩形而不是使用参数legend 添加标题并通过反复试验搜索图片中心的位置是为标题。

  2. 为什么 .png 与 R 窗口中显示的不同?

【问题讨论】:

  • 1) 见?par中的“asp”; 2) 两种不同的设备
  • 感谢您的评论。是的,我确实偶然发现了这件事并尝试了但没有成功。 asp 不是图形参数,所以我不认为我可以在 par(..) 中使用它,并且在 image(..) 中使用它似乎没有任何影响(虽然不会引发任何错误)。
  • 我的错误,asp 在 plot.window 下,但无论哪种方式,您都可以通过 image.. 传递它。哦,您想要一个矩形。你能发布一个工作示例吗
  • 我想我不完全理解asp 的工作原理。为什么以下内容没有水平重塑:image(matrix(1:10, ncol=1), asp=0.1)
  • png 中有 widthheight 参数。

标签: r graphics plot


【解决方案1】:

我可以做到这一点:

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    相关资源
    最近更新 更多