无法访问和存储绘图

时间:2014-01-13 12:56:39

标签: r plot

我尝试获取内核判别分析的核密度估计图,我使用下一个代码:

library("MASS")
install.packages("rgl")
install.packages("misc3d")
install.packages("mvtnorm")
install.packages("KernSmooth")
install.packages("ks")
library("ks")
library("MASS")
library("ks")
data("iris")
ir <- iris[,1:3]
ir.group <- iris[,5]
Hpi1 <- Hkda(x = ir, x.group = ir.group, bw = "plugin",
pilot = "samse", pre = "sphere")
Hpi2 <- Hkda.diag(x = ir, x.group = ir.group, bw = "plugin",
pilot = "samse", pre = "scale")
Hscv1 <- Hkda(x = ir, x.group = ir.group, bw = "scv", pre = "sphere")
Hscv2 <- Hkda.diag(x = ir, x.group = ir.group, bw = "scv", pre = "scale")
#As of ks 1.8.11, kda.kde has been subsumed into kda, so all prior calls to kda.kde can be replaced
#by kda. To reproduce the previous behaviour of kda, the command is kda(, kde.flag=FALSE)
#
kda(x = ir, x.group = ir.group, Hs = Hpi1)

kda(x = ir, x.group = ir.group, Hs = Hpi1, kde.flag=FALSE)

install.packages("scatterplot3d")
library(scatterplot3d)
scatterplot3d(iris[,1:3],color=c("red","blue","green")
[iris$Species], col.axis="blue", col.grid="lightblue",
main="scatterplot3d",pch=20,cex.symbols=2)
iris.f1<-kda(x = ir, x.group = ir.group, Hs = Hpi1)
iris.f2<-kda(x =ir,x.group = ir.group, Hs = Hscv1)

plot(iris.f1)
plot(iris.f2)

但是当我到达plot(iris.f1)时,rgl设备会在几秒钟内向我显示四个图形,我无法全部获取它们。我尝试使用par(mfrow=c(2,2)),但它没有帮助。 请帮助我理解,为什么我不能得到所有这些图形,我做错了什么,或者我应该做些什么来获得所有这些图形。

1 个答案:

答案 0 :(得分:0)

该图表生成所需的图形,问题是它们在生成新图形时关闭。您可以通过打开pdf并存储图表来存储它们。

pdf("Plots1.pdf", title="Plots Iri 1") #Opens the pdf
plot(iris.f1)
dev.off() #To close the opened pdf
pdf("Plots2.pdf", title="Plots Iri 2")
plot(iris.f2)
dev.off() #To close the opened pdf

这将创建2个pdf,工作文件夹中的每个页面都有一个图表(要知道哪个是类型getwd()


您找到了以下内容:

for(i in 1:3){ #Previously there are three different plots
   pdf(paste("plot", i,".pdf",sep=""))#Creates the pdf
   plot(cbind(iris[1],iris[i])) #Join all the plots in one
   dev.off() } #Close the pdf and the iteration

这段代码所做的是联合存储在pdf中的图,以适应您的代码,也许您需要执行以下操作,尽管它与我之前发布的几乎相同:

pdf("Plots.pdf", title="Plots Iris")
plot(cbind(iris.f1, iris.f2))
dev.off()