[R] Histograms of lots of variables

Petr Klasterecky klaster at karlin.mff.cuni.cz
Tue Apr 17 08:00:11 CEST 2007


Please, experiment a little bit :-)
I have trouble using Win metafile graphics, so I just converted it to 
jpeg. The code works - there was a copy/paste error. Try this with your 
'total' dataframe - it works with my 'total' given there:

total <- list(a=rnorm(1000),b=rnorm(1000),c=(10*runif(1000)-5))
lapply(1:3, function(.ind){
jpeg(filename=paste("file.", .ind, ".jpg", sep=''))
hist(total[[.ind]], main=paste('Histogram of',names(total)[.ind], sep=" 
"), xlab="")
dev.off()
})

You can add the breaks argument inside the call of hist() as you would 
normally do when plotting a histogram. However, consider it carefully - 
it will most probably result into an error if your breaks won't fit all 
of your data.

Petr

mtb954 at gmail.com napsal(a):
> Thanks again Jim, I appreciate your time. I've been trying to debug the
> code, but am running into a wall. I'm getting a syntax error after the line
> containing the hist function. Here's the R session, any ideas?
> 
> Also, I'd like to be able to have each histogram use the same x-axis breaks
> (0, 1, 2-3, 4-10, 11-20, >20) using the breaks argument, but it's not clear
> to me where I'd put that in your code.
> 
> Thanks! Mark
> 
> 
> 
> 
>> total<-read.csv("total_ec.csv") #creates a dataframe called total from a
> .csv file
>> lapply(10:20, function(.ind){
> +    win.metafile(filename=paste("file.", .ind, ".wmf", sep=''))
> +    hist(total[[.ind]], main=colnames(total)[.ind], xlab="")
> + sep=''), xlab="")
> Error: syntax error in:
> "   hist(total[[.ind]], main=colnames(total)[.ind], xlab="")
> sep='')"
>>    dev.off()
> Error in dev.off() : cannot shut down device 1 (the null device)
>> })
> 
> 
> 
> 
> 
> 
> On 4/16/07, jim holtman <jholtman at gmail.com> wrote:
>> This will put the colnames on the histograms:
>>
>> lapply(3:20, function(.ind){
>>     win.metafile(filename=paste("file.", .ind, ".wmf", sep=''))
>>     hist(dataframe[[.ind]], main=colnames(dataframe)[.ind], xlab="")
>>     dev.off()
>> })
>>
>>
>> On 4/16/07, mtb954 at gmail.com <mtb954 at gmail.com> wrote:
>>> Hi Jim, thanks for your help. That looks like it might work, but a
>> couple of
>>> things...
>>>
>>> 1) The resulting 73 (in my case) histograms will be named by their
>> variable
>>> number, not by the variable name contained in the first row. Any way to
>>> include the variable name in the resulting histogram?
>>>
>>> 2) How can I avoid having the resulting "output.wmf" files overwrite
>> each
>>> other? I'd like to keep all of them, not just the last one.
>>>
>>> Any ideas?
>>>
>>> Thanks, Mark
>>>
>>>
>>>
>>>
>>> On 4/16/07, jim holtman <jholtman at gmail.com> wrote:
>>>> try:
>>>>
>>>> win.metafile(file="output.wmf")
>>>> lapply(dataframe[3:20], hist)
>>>> dev.off()
>>>>
>>>> On 4/16/07, mtb954 at gmail.com <mtb954 at gmail.com > wrote:
>>>>> Hi R-helpers,
>>>>>
>>>>> I wish to produce frequency histograms of all of the variables in my
>>>>> dataframe (except some identifying variables).
>>>>>
>>>>> I have tried
>>>>>
>>>>>> hist(dataframe[,3:20])
>>>>> to produce histograms of the 3rd through 20th variables in my
>> dataframe,
>>> but
>>>>> R doesn't like that.
>>>>>
>>>>> Could anyone provide a suggestion?
>>>>>
>>>>> Also, once I produce the histograms, I'd like to save them as
>> graphic
>>> files
>>>>> on my computer. How would I do that using code (rather than
>> Right-click
>>> |
>>>>> Save as metafile, which would be tedious for dozens of histograms).
>>>>>
>>>>> Thanks, Mark
>>>>>
>>>>>        [[alternative HTML version deleted]]
>>>>>
>>>>> ______________________________________________
>>>>> R-help at stat.math.ethz.ch 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.
>>>>>
>>>>
>>>> --
>>>> Jim Holtman
>>>> Cincinnati, OH
>>>> +1 513 646 9390
>>>>
>>>> What is the problem you are trying to solve?
>>>>
>>>
>>
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem you are trying to solve?
>>
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 

-- 
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic



More information about the R-help mailing list