【问题标题】:R function to create and save graphicR函数创建和保存图形
【发布时间】:2015-05-13 21:22:28
【问题描述】:

我正在尝试创建和保存几个图形。我被困在factoextra 包用于制作图表的情况下。

  pca.plot<-function(x){
  biplot<-paste(out_f,"\\biplot.jpg", sep="")
  jpeg(file=biplot, type="cairo")
  fviz_pca_biplot(x,  geom = "text")
  dev.off()
}

这是一个从输入 pca 对象(使用 FactoMineR 包创建的 pca)创建双标图的简单函数 out_f 是先前定义的。 当我逐行运行脚本时,它可以工作。当我将它作为脚本运行时,什么都没有创建。

  pca.plot<-function(x){
  pve<-paste(out_f,"\\proportion_of_variance_explained.jpg", sep="")
  jpeg(file=pve, type="cairo")
  barplot(x$eig[,2], names.arg=1:nrow(x$eig),
          main = "Variances",
          xlab = "Principal Components",
          ylab = "Percentage of variances",
          col ="steelblue")
  lines(x = 1:nrow(x$eig), x$eig[, 2], type="b", pch=19, col = "red")
  dev.off()   
}

在这种情况下没有问题。有谁知道为什么第一种情况有问题?

提前致谢, 约翰

【问题讨论】:

  • fviz_pca_biplot 生成 ggplot2 图。除非您在它们上调用print,否则它们实际上不会被绘制出来。 (在自动发生的命令行上。)

标签: r graph factoextra


【解决方案1】:

factoextra 生成的图是 ggplot2。你应该使用 print(fviz_pca_biplot(res.pca)) 如下:

# Principal component analysis
data(iris)
res.pca <- prcomp(iris[, -5],  scale = TRUE)

# This will do nothing
jpeg("biplot.jpg")
fviz_pca_biplot(res.pca)
dev.off()

# This will do the right thing
jpeg("biplot.jpg")
print(fviz_pca_biplot(res.pca))
dev.off()

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    相关资源
    最近更新 更多