0

I have a problem exporting some statistical plot on R. I am using the function jpeg() with the function dev.off()...I have replicated the code and it gives me the same error:

data1<-rnorm(n = 1000,mean = 0,sd = 1)
data<-cbind(data,data,data,data)

for(i in c(1,2,3)){
  print(i)
  if(i ==1){
    #Histograms and density
    jpeg(paste(getwd(),'/Hist_',i,'.jpg',sep=''))
    histogram( data[,1],xlab = "data" )
    dev.off()
    #boxplot
    jpeg(paste(getwd(),'/Boxplot_',i,'.jpg',sep=''))
    boxplot(data[,1],xlab='cluster', ylab='data')
    dev.off()
  }else{
    #Histograms and density
    jpeg(paste(getwd(),'/Hist_',i,'.jpg',sep=''))
    histogram( data[,1],xlab = "data" )
    dev.off()
    #boxplot
    jpeg(paste(getwd(),'/Boxplot_',i,'.jpg',sep=''))
    boxplot(data[,1],xlab='cluster', ylab='data')
    dev.off()
  }
}

if I set i=1 and then I run the relative cycle I have no problems, but if I run the entire cycle R exports only the boxplot images....does anyone know why? thank you !

Bmb58
  • 155
  • 2
  • 9
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. It's much easier to help if we can copy/paste the code into R to test it. It's possible your approximations have removed the actual problem. – MrFlick Mar 13 '18 at 16:13
  • I have replicated the problem! – Bmb58 Mar 13 '18 at 16:27

1 Answers1

2

The only problem in your code is data<-cbind(data,data,data,data) where it show be data<-cbind(data1,data1,data1,data1) and the fact that histogram is not base-r function. If you fix above, and set histogram=hist, everything works fine.

  • Or if you really want to use `histogram()` rather than `hist()`, use `print(histogram( data[,1],xlab = "data" ))` to make it draw. – MrFlick Mar 13 '18 at 19:16
  • It works! This example is simplified, I need to create an histogram of data for each level of another variable (cluster), so I think the function histogram() is better...print() makes it work! Thank you! – Bmb58 Mar 14 '18 at 10:23