Skip to content

Plotting Histogram using histogram() and for loop and I want to save the histogram individually ... HELP

12 messages · ychu066, Karl Ove Hufthammer, Colin Millar +2 more

#
I am trying to write R codes for the histogram plot and saving it out.

There will be 146 histogram plots in total?.

I have the following R codes that draws the each Histogram ?.
library(lattice)
for(i in 8:153){ 
histogram(~ data[,i] | data[,2],
data=data,,ylab="Frequency",xlim=c(1,5),xlab="Score",ylim=c(0,100)),main=paste(colnames(data)[i],sep="")
}


And I also want to save each histogram in each separate pdf file using the
following codes ?.
png("hist.png[i]")
dev.off()

But my R coding does NOT work ?. I need someone to help me with this ??.
Thanks million
#
On Thu, 12 Nov 2009 19:10:52 -0800 (PST) ychu066 <ychu066
@aucklanduni.ac.nz> wrote:
Try png(paste("hist",i,".png",sep="") instead.
3 days later
#
still doesnt work ...
Karl Ove Hufthammer wrote:

  
    
  
#
here is the codes that i tried.....
+ library(lattice)
Error: unexpected symbol in:
"png(paste("hist",i,".png",sep="") 
library"
+ histogram(~ data[,i] | data[,2],
data=data,ylab="Frequency",xlim=c(1,5),xlab="Score",ylim=c(0,100)))
Error: unexpected ')' in:
"for(i in 8:153){ 
histogram(~ data[,i] | data[,2],
data=data,ylab="Frequency",xlim=c(1,5),xlab="Score",ylim=c(0,100)))"
Error: unexpected '}' in "}"
Error in dev.off() : cannot shut down device 1 (the null device)
ychu066 wrote:

  
    
  
#
tried but still doesnt work ...

very weird ...
ychu066 wrote:

  
    
  
#
On Mon, 16 Nov 2009 19:54:08 -0800 (PST) ychu066 <ychu066
@aucklanduni.ac.nz> wrote:
There is a missing ')' at the end of the first line.
If you use an editor with syntax highlighting, it is
easy to spot these types of errors.
#
Or alternatively store as a list and export later if you want

... after some tidying ...


library(lattice)

columns <- 8:153
plots <- vector("list", length(columns)) 
j <- 0
for (i in columns)
{  
  plots[[ j <- j+1 ]] <- 
    histogram( ~ data[,i] | data[,2], 
      ylab = "Frequency", xlab = "Score", 
      xlim = c(1,5), ylim = c(0,100),
      main = colnames(data)[i]
    )
}

print(plots[[1]]) 

# or export

for (i in seq_along(plots))
{
  png(paste("hist", i, ".png", sep = ""))
  print(plots[[i]])
  dev.off()
}

HTH
Colin.

Incidentally, 

You put what you want to export between png(..) and dev.off()

If you supply the data explicitly it doesn't make any sense to pass the
data through the data argument.

No need for paste(x) if is x is already a character vector.


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Tal Galili
Sent: 17 November 2009 12:15
To: ychu066
Cc: r-help at r-project.org
Subject: Re: [R] Plotting Histogram using histogram() and for loop and
Iwant to save the histogram individually ... HELP

I know how you feel,
I came a cross the same problem once, which took sometime to find a
solution
for.

What you need to do is put the hist into a variable and then plot it,
for
example:



library(lattice)
for(i in 8:153){

hist.to.plot <- histogram(~ data[,i] | data[,2],
data=data,,ylab="Frequency",xlim=c(1,5),xlab="Score",ylim=c(0,100)),main
=paste(colnames(data)[i],sep="")
plot(hist.to.plot)
}


Cheers,
Tal


----------------------------------------------


My contact information:
Tal Galili
E-mail: Tal.Galili at gmail.com
Phone number: 972-52-7275845
FaceBook: Tal Galili
My Blogs:
http://www.talgalili.com (Web and general, Hebrew)
http://www.biostatistics.co.il (Statistics, Hebrew)
http://www.r-statistics.com/ (Statistics,R, English)




On Tue, Nov 17, 2009 at 7:09 AM, ychu066 <ychu066 at aucklanduni.ac.nz>
wrote:
using
http://old.nabble.com/Plotting-Histogram-using-histogram%28%29-and-for-l
oop-and-I-want-to-save-the-histogram-individually-...-HELP-tp26328734p26
384489.html
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
#
THANKS EVERYONE, IT WORKS NOW !!!

BY THE WAY, i want to improve my R coding skills, any suggestion for me ?
Colin Millar wrote:
:working::working::working::working::working::working:
#
It's a FAQ.

http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f

--  
David.
On Nov 17, 2009, at 7:14 AM, Tal Galili wrote:

            
#
Hi again, do you know how can I use the main function to change the title for
eac h histogram. i.e that first graph show have a title "Index 1" but
because the variable name column starts the 8th the title shows up as "Index
8" , i have tried use main = colnames(data)[i-1] but it result errors....
Colin Millar wrote: