[R] Possible to "import" histograms in R?

(Ted Harding) Ted.Harding at manchester.ac.uk
Wed Aug 15 12:53:21 CEST 2007


On 15-Aug-07 10:15:13, Nick Chorley wrote:
>>[...]
>> Now create a histogram template (any silly old data will do):
>>
>> H1 <- hist(c(1,2))
>>
>> Next, attach your variables to it:
>>
>> H1$breaks <- brkpts
>> H1$counts <- counts
>>
>> and you have your histogram in R. Also, you can use the data
>> in the variables 'brkpts', 'counts' to feed into any other
>> procedure which can acept data in this form.
> 
> 
> This is precisely what I wanted to do, except I didn't realise
> that you could assign to the variables in the histogram object
> like this. 
> Normally when constructing the histogram, I'd use hist(x, prob=T)
> to plot the probability density against x, but obviously if you're
> just assigning values to the variables in the object, you can't do
> that. I tried putting "prob=T" in the call to hist when making the
> dummy object, but that didn't help.

Followup:

The histogram object I constructed in the example from my previous
reply contains the following:

H1
$breaks
 [1]  0.0  0.5  1.0  1.5  2.0  2.5  3.0  3.5  4.0  4.5  5.0
[12]  5.5  6.0  6.5  7.0  7.5  8.0  8.5  9.0  9.5 10.0

$counts
 [1]  3  6  3  7  7  8 19 10 16 12 16 12 13 12 11 13 11  7  3  6

$intensities
[1] 0.9999992 1.0000000

$density
[1] 0.9999992 1.0000000

$mids
[1] 1.25 1.75

$xname
[1] "c(1, 2)"

$equidist
[1] TRUE

All these things are calculated when hist() is called for
raw data.

The "$breaks" and "$counts" are what I assigned to it with

  H1$breaks <- brkpts    H1$counts <- counts

"$intensities", "$density", "$mids", "$xname" and "$equidist"
are what was set up by the initial call

H1 <- hist(c(0,1))

Previously, I used "plot(H1)" to illustrate that the above
assignments put the breakpoints and the counts into the
histogram object. For a more thorough approach, you need to
use plot.histogram() instead.

Here you can, in fact, set the "prob=TRUE" condition in the
plot by setting "freq=FALSE" in t call to plot.histogram().

But then it will look at "$density" for the values to plot.

So you if you want the density plot, you would need to calculate
this for yourself. E.g.

H1$density <- counts/sum(counts)
plot.histogram(H1,freq=FALSE)

And so on ... there are many relevant details in the help pages
?hist and ?plot.histogram

Best wishes,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 15-Aug-07                                       Time: 11:53:14
------------------------------ XFMail ------------------------------



More information about the R-help mailing list