温馨提示:本文翻译自stackoverflow.com,查看原文请点击:r - How can I integrate the "Export --> Save as Image" Click Process into a script?
r

r - 如何将“导出->另存为图像”单击“处理”集成到脚本中?

发布于 2020-03-29 12:52:44

我有一个定义4个变量并最终产生它们的图形表示的脚本。每次我运行脚本时,表示形式都是不同的。

我想添加一行代码,以便每个表示形式都保存为png文件(或jpg),并且不会被下一次迭代中的下一个图像覆盖。

感谢您的任何建议。

码:

f1=jitter(sample(c(2,3),1));
f2=jitter(sample(c(2,3),1));
f3=jitter(sample(c(2,3),1));
f4=jitter(sample(c(2,3),1));
d1=runif(1,0,1e-02);
d2=runif(1,0,1e-02);
d3=runif(1,0,1e-02);
d4=runif(1,0,1e-02)
p1=runif(1,0,pi);
p2=runif(1,0,pi);
p3=runif(1,0,pi);
p4=runif(1,0,pi);
xt = function(t) exp(-d1*t)*sin(t*f1+p1)+exp(-d2*t)*sin(t*f2+p2)
yt = function(t) exp(-d3*t)*sin(t*f3+p3)+exp(-d4*t)*sin(t*f4+p4)
t=seq(1, 100, by=0.001)

dat=data.frame(t=t, x=xt(t), y=yt(t)) 
with(dat, plot(x,y, type="l", xlim =c(-2,2), ylim =c(-2,2), xlab = "", ylab = "", xaxt='n', yaxt='n'))

查看更多

查看更多

提问者
TomTe
被浏览
19
Gowachin 2020-01-31 16:29

这是尝试回答您的问题。我测试了您的代码,至少它可以在Ubuntu 18.04上运行。

# your code up

# create the file
png(filename = "test.png", width = 480, height = 480, units = "px", pointsize = 12, bg = "white", res = NA, type = c("cairo", "cairo-png", "Xlib", "quartz"))
# plot
with(dat, plot(x,y, type="l", xlim =c(-2,2), ylim =c(-2,2), xlab = "", ylab = "", xaxt='n', yaxt='n'))
# close the file
dev.off()