生成与R Studio输出不等效的图像文件

生成与R Studio输出不等效的图像文件,r,plot,histogram,R,Plot,Histogram,我使用下面的R代码生成数据帧属性的直方图。直方图的标题是文件中df[[i]]的直方图,而R Studio显示正确的“x直方图”或“y直方图”。我做错了什么 df <- data.frame(x=runif(20), y=runif(20)) analyzedata <- function(df){ for(i in names(df)){ fit <- hist(df[[i]],main=paste("Histogram of" , i)) png(file

我使用下面的R代码生成数据帧属性的直方图。直方图的标题是文件中df[[i]]的直方图,而R Studio显示正确的“x直方图”或“y直方图”。我做错了什么

df <- data.frame(x=runif(20), y=runif(20))
analyzedata <- function(df){
  for(i in names(df)){
    fit <- hist(df[[i]],main=paste("Histogram of" , i))
    png(filename=paste("/home/jabro/histogram_",i,".png"))
    plot(fit)
    dev.off()
  }
}

df这个hist函数做两件事。它计算直方图数据并绘制数据。当您指定返回值时,即
fit
analyzedata <- function(df){
  for(i in names(df)){
    fit <- hist(df[[i]],main=paste("Histogram of" , i))
    fit$xname < - i
    png(filename=paste("/home/jabro/histogram_",i,".png"))
    plot(fit)
    dev.off()
  }
}
analyzedata <- function(df){
  for(i in names(df)){
    fit <- hist(df[[i]],main=paste("Histogram of" , i))
    png(filename=paste("/home/jabro/histogram_",i,".png"))
    plot(fit,main=paste("Histogram of" , i))
    dev.off()
  }
}