GlenCloncurry GlenCloncurry - 27 days ago 8
R Question

Obtain the value from Histogram and divide it by value from second Histogram

I would like to add an abline to a histogram which shows the value from the first histogram (data) divided by the value from the second histogram (Survived).

hist(data$test, col='blue', main="DataPoint", xlim=c(0,600), ylim=c(0,800), breaks=c(seq(0,600,50)))
hist(Survived$test, col='green', breaks=c(seq(0,600,50)), add=TRUE)


Im trying to get something like the below onto the histogram. Any assistance would be appreciated.

abline(count(data$test)/count(Survived$test), breaks = c(seq(0,600,50)), add = TRUE)

Answer Source

It is possible to assign the output from hist to an object, like so:

> x <- hist(1:10)

> x
$breaks
[1]  0  2  4  6  8 10

$counts
[1] 2 2 2 2 2

$density
[1] 0.1 0.1 0.1 0.1 0.1

$mids
[1] 1 3 5 7 9

$xname
[1] "1:10"

$equidist
[1] TRUE

attr(,"class")
[1] "histogram"

You can then access the values, e.g. with x$density or x$counts.