<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/hacking-highcharter-observations-per-group-in-boxplots/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://nsaunders.wordpress.com/2017/07/24/hacking-highcharter-observations-per-group-in-boxplots/"> R – What You're Doing Is Rather Desperate</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://www.highcharts.com/" target="_blank">Highcharts</a> has long been a favourite visualisation library of mine, and I’ve written before about <a rel="nofollow" href="http://jkunst.com/highcharter/" target="_blank">Highcharter</a>, my preferred way to use Highcharts in R.</p>
<p>Highcharter has a nice simple function, <code>hcboxplot()</code>, to generate boxplots. I recently generated some for a project at work and was asked: can we see how many observations make up the distribution for each category? This is a common issue with boxplots and there are a few solutions such as: overlay the box on a jitter plot to get some idea of the number of points, or try a violin plot, or a so-called bee-swarm plot. In Highcharts, I figured there should be a method to get the number of observations, which could then be displayed in a tool-tip on mouse-over.</p>
<p>There wasn’t, so I wrote one like this.<br>
<span></span></p>
<p>First, you’ll need to install <code>highcharter</code> <a rel="nofollow" href="https://github.com/jbkunst/highcharter" target="_blank">from Github</a> to make it work with the latest <code>dplyr</code>.</p>
<p>Next, we generate a reproducible dataset using the <code><a rel="nofollow" href="https://github.com/trinker/wakefield" target="_blank">wakefield</a></code> package. For some reason, we want to look at age by gender, but only for redheads:</p>
<pre>
library(dplyr)
library(tidyr)
library(highcharter)
library(wakefield)
library(tibble)

set.seed(1001)
sample_data &lt;- r_data_frame(
  n = 1000,
  age(x = 10:90),
  gender,
  hair
) %>%
  filter(hair == &quot;Red&quot;)

sample_data %>%
  count(Gender)

## # A tibble: 2 x 2
##   Gender     n
##   </pre>