in R I'm trying to interactively identify bin value in a histogram using the mouse. I think I need something equivalent to the identify() function for scatterplots. But identify() doesn't seem to work for histograms.
in R I'm trying to interactively identify bin value in a histogram using the mouse. I think I need something equivalent to the identify() function for scatterplots. But identify() doesn't seem to work for histograms.
Use locator() to find the points, then lookup which interval the value sits in, make sure it is less than the y-value for the bar, then return the count:
set.seed(100)
h <- hist(rnorm(1:100))
# use locator() when doing this for real, i'm going to use a saved set of points
#l <- locator()
l <- list(x = c(-2.22, -1.82, -1.26, -0.79,-0.57, -0.25, 0.18, 0.75,
0.72, 1.26), y = c(1.46, 7.81, 3.79, 9.08, 17.11, 11.61, 15,
17.96, 5.9, 3.37))
# for debugging purposes - the nth value of the output should match where
# the n value is shown on the histogram
text(l, labels=1:10, cex=0.7, font=2)
fi <- findInterval(l$x, h$breaks)
sel <- (l$y > 0) & (l$y < h$counts[fi])
replace(h$counts[fi], !sel, NA)
#[1] 3 NA 9 14 NA 22 20 NA 13 7