Displaying Distributions
Most of the exercises on d3aday were inspired by complete data visualizations. This one was somewhat inspired by a particular look and feel, but was mostly inspired by the data itself. Along the way, though, you can stay sharp on your data munging and formatting skills, and create an interesting graph with some Covid-19 related data.
The Inspiration
The inspiration for this exercise comes from the New York Times and Dynata’s July 2020 survey on public mask-wearing. They published data for counties across the U.S. that answered the question, “How often do you wear a mask in public when you expect to be within six feet of another person?” The Times’s analysis of this data is fascinating and quite beautiful in its own right.
The data can be found in full here.
We took a piece of the data - respondents who say they “always” wear a mask in public - and set out to use it to create a graph that compared different states. You can find our streamlined csv (with state designations added) here.
The visual inspiration
This “dot graph,” also known as a dot strip graph or strip graph, is a way to visualize data points for different categories in a compact form. The emphasis here is on the distribution of the points across the horizontal - you can easily see how clumped together or spread out they are in each row.
We wanted to recreate this look in showing how varied the mask survey results are for each state.
The Exercise
For our version of this viz, we wanted to keep things pretty standard. Each state gets a row, and each row shows the percentage responses for that state’s data as circles.
The X-axis should be a range from 0% to 100%.
We also wanted to sort the states in order of average percentage, rather than something arbitrary like alphabetical. That will make it easier to see where each state ranks overall in mask usage.
The data can be found here.
One hint - I found it easier to sort things by creating an object for each state, adding the average to the object, then sorting all the objects in an array. Something like this:
I’ll leave it up to the reader to write the groupbystate
and maptoobject
functions. Or you can just scroll down a little.
A Solution
My solution looks like this:
The solution code is below, but as always you’re encouraged to try writing it yourself before peeking. The complete code (with HTML and CSS) is available on codepen.