Another day, another interesting challenge.
I follow Bob Rudis’ (a.k.a. hrbrmstr’s) blog, typically via R-bloggers, and this post caught my eye. Partly because I thought I knew of an existing way to do this. As usual, actually getting that to work took a little longer than I might have hoped, but I think the end result is pretty neat.
His post describes the process of writing an R function to take an image file, for example this one
and producing a histogram along the sides of the number of pixels on a given row/column. This is what he created (a different image to the example, I believe)
Something funny is going on with the right-hand histogram; it doesn’t line up with the image.
Here’s my approach.
It leverages the
png package to extract the channels into a matrix, converts those to
x,y,z data.frames, takes the median value, plots that with
ggplot2, then leverages
ggExtra::ggMarginal to add the marginal histograms. Note that the
ggExtra package has some bugs (it hasn’t been maintained in a while) in relation to more recent (possibly the dev branch) of
ggplot2. I got it working on at least one of my machines. This is my result
I’ve had several uses for these types of marginal plots lately, so hopefully I can sort out the issues I’ve been getting in combination with