1

I have been trying to get a modified beeswarm plot up and running. What is "special" about it is I'm trying to split the y values based on a boolean. So you can imagine a kind of double beeswarm visual -- one section has circles appended above the x axis, while the other section has the circles appended below the x axis.

My data is very simple, but for context here is an explanation:

  • d.educ = education (discrete variable, in years)
  • d.inlf = in labor force (1 if yes, 0 if no)

So, if we're clear on that, let me reiterate my goal. I want the circles where the observation is in the labor force to be above the x axis, and I want the circles where the observation is not in the labor force to be below the x axis.

Code below with 150 sample from my full data set (hard-coded):

var margins = {top:20, bottom:300, left:30, right:100};

var height = 200;
var width = 900;

var totalWidth = width+margins.left+margins.right;
var totalHeight = height+margins.top+margins.bottom;

var svg = d3.select('body')
    .append('svg')
    .attr('width', totalWidth)
    .attr('height', totalHeight);

var graphGroup = svg.append('g')
    .attr('transform', "translate("+margins.left+","+margins.top+")");

    var formatValue = d3.format(",d");

    var xScale = d3.scaleLinear()
        .range([0, width]);
/*
var tsvData = d3.tsv('voronoi-circles.tsv');

tsvData.then(function(rawData) {



  var data = rawData.map(function(d) {
    return {educ:+d.educ, inlf:+d.inlf}
  });
*/

var data = [{'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 14.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 16.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 11, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 10.0, 'inlf': 1},
 {'educ': 11, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 16.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 13.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 17.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 17.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 11, 'inlf': 1},
 {'educ': 16.0, 'inlf': 1},
 {'educ': 13.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 16.0, 'inlf': 1},
 {'educ': 11, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 10.0, 'inlf': 1},
 {'educ': 14.0, 'inlf': 1},
 {'educ': 17.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 16.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 16.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 8.0, 'inlf': 1},
 {'educ': 10.0, 'inlf': 1},
 {'educ': 16.0, 'inlf': 1},
 {'educ': 14.0, 'inlf': 1},
 {'educ': 17.0, 'inlf': 1},
 {'educ': 14.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 14.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 8.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 8.0, 'inlf': 1},
 {'educ': 17.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 1},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 9.0, 'inlf': 0},
 {'educ': 10.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 17.0, 'inlf': 0},
 {'educ': 15.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 6.0, 'inlf': 0},
 {'educ': 14.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 14.0, 'inlf': 0},
 {'educ': 9.0, 'inlf': 0},
 {'educ': 17.0, 'inlf': 0},
 {'educ': 13.0, 'inlf': 0},
 {'educ': 9.0, 'inlf': 0},
 {'educ': 15.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 13.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 13.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 16.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 13.0, 'inlf': 0},
 {'educ': 10, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 17.0, 'inlf': 0},
 {'educ': 14.0, 'inlf': 0},
 {'educ': 16.0, 'inlf': 0},
 {'educ': 17.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 10, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 17.0, 'inlf': 0},
 {'educ': 10.0, 'inlf': 0},
 {'educ': 13.0, 'inlf': 0},
 {'educ': 10, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 16.0, 'inlf': 0},
 {'educ': 17.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 16.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 16.0, 'inlf': 0},
 {'educ': 8.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 13.0, 'inlf': 0},
 {'educ': 11.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 14.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 17.0, 'inlf': 0},
 {'educ': 14.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 9.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 12.0, 'inlf': 0},
 {'educ': 14.0, 'inlf': 0},
 {'educ': 16.0, 'inlf': 0}];

  xScale.domain(d3.extent(data, function(d) { return d.educ; }));

  var simulation = d3.forceSimulation(data)
    .force("x", d3.forceX(function(d) { return xScale(d.educ); }).strength(1))
    .force("y", d3.forceY(height / 2))
    .force("collide", d3.forceCollide(4))
    .stop();

    for (var i = 0; i < 120; ++i) simulation.tick();

    graphGroup.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(xScale).ticks(20, ".0s"));

  var cell = graphGroup.append("g")
      .attr("class", "cells")
    .selectAll("g").data(d3.voronoi()
        .extent([[-margins.left, -margins.top], [width + margins.right, height + margins.top]])
        .x(function(d) { return d.x; })
        .y(function(d) {
          if (d.inlf==1) {
            return d.y;
          } else {
            return d.y+300;
          }
          })
      .polygons(data)).enter().append("g");

      cell.append("circle")
          .attr("r", 3)
          .attr("cx", function(d) { return d.data.x; })
          .attr("cy", function(d) { return d.data.y; });

      cell.append("path")
          .attr("d", function(d) { return "M" + d.join("L") + "Z"; });

/*
      cell.append("title")
          .text(function(d) { return d.data.id + "\n" + formatValue(d.data.value); });
*/
//})
.cells path {
  fill: none;
  pointer-events: all;
}

.cells :hover circle {
  fill: red;
}
<script src="https://d3js.org/d3.v5.min.js"></script>

As you can see, I tried a fairly straight-forward approach, just slapping on a +300 to the y value using an if statement. However, the result was not as expected, and seems to have truncated all the values to 0. No clue why.

Question

How can I modify the circle coordinate logic to account for my bool (d.inlf)? Or is it not that simple; perhaps another separate d3.voronoi() should be called. In either case, I'm not sure how to proceed.

Marcus Campbell
  • 2,746
  • 4
  • 22
  • 36
Arash Howaida
  • 2,575
  • 2
  • 19
  • 50

1 Answers1

3

I have absolutely no idea what's your purpose using a voronoi here, so I just got rid of it.

You need just two things. First, you determine the y position on the simulation:

.force("y", d3.forceY(function(d) {
    return d.inlf ? height - 100 : height + 100
}))

Then, you set the circles' positions:

.attr("cx", function(d) {
    return d.x;
})
.attr("cy", function(d) {
    return d.y;
});

Finally, have in mind that there is a shorthand for how many times a simulation's tick must run, so this...

for (var i = 0; i < 120; ++i) simulation.tick();

... can be just:

simulation.tick(120);

Here is the demo:

var margins = {
  top: 0,
  bottom: 200,
  left: 30,
  right: 100
};

var height = 150;
var width = 900;

var totalWidth = width + margins.left + margins.right;
var totalHeight = height + margins.top + margins.bottom;

var svg = d3.select('body')
  .append('svg')
  .attr('width', totalWidth)
  .attr('height', totalHeight);

var graphGroup = svg.append('g')
  .attr('transform', "translate(" + margins.left + "," + margins.top + ")");

var formatValue = d3.format(",d");

var xScale = d3.scaleLinear()
  .range([0, width]);

var data = [{
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 14.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 16.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 11,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 10.0,
    'inlf': 1
  },
  {
    'educ': 11,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 16.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 13.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 17.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 17.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 11,
    'inlf': 1
  },
  {
    'educ': 16.0,
    'inlf': 1
  },
  {
    'educ': 13.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 16.0,
    'inlf': 1
  },
  {
    'educ': 11,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 10.0,
    'inlf': 1
  },
  {
    'educ': 14.0,
    'inlf': 1
  },
  {
    'educ': 17.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 16.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 16.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 8.0,
    'inlf': 1
  },
  {
    'educ': 10.0,
    'inlf': 1
  },
  {
    'educ': 16.0,
    'inlf': 1
  },
  {
    'educ': 14.0,
    'inlf': 1
  },
  {
    'educ': 17.0,
    'inlf': 1
  },
  {
    'educ': 14.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 14.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 8.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 8.0,
    'inlf': 1
  },
  {
    'educ': 17.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 1
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 9.0,
    'inlf': 0
  },
  {
    'educ': 10.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 17.0,
    'inlf': 0
  },
  {
    'educ': 15.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 6.0,
    'inlf': 0
  },
  {
    'educ': 14.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 14.0,
    'inlf': 0
  },
  {
    'educ': 9.0,
    'inlf': 0
  },
  {
    'educ': 17.0,
    'inlf': 0
  },
  {
    'educ': 13.0,
    'inlf': 0
  },
  {
    'educ': 9.0,
    'inlf': 0
  },
  {
    'educ': 15.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 13.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 13.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 16.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 13.0,
    'inlf': 0
  },
  {
    'educ': 10,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 17.0,
    'inlf': 0
  },
  {
    'educ': 14.0,
    'inlf': 0
  },
  {
    'educ': 16.0,
    'inlf': 0
  },
  {
    'educ': 17.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 10,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 17.0,
    'inlf': 0
  },
  {
    'educ': 10.0,
    'inlf': 0
  },
  {
    'educ': 13.0,
    'inlf': 0
  },
  {
    'educ': 10,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 16.0,
    'inlf': 0
  },
  {
    'educ': 17.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 16.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 16.0,
    'inlf': 0
  },
  {
    'educ': 8.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 13.0,
    'inlf': 0
  },
  {
    'educ': 11.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 14.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 17.0,
    'inlf': 0
  },
  {
    'educ': 14.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 9.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 12.0,
    'inlf': 0
  },
  {
    'educ': 14.0,
    'inlf': 0
  },
  {
    'educ': 16.0,
    'inlf': 0
  }
];

xScale.domain(d3.extent(data, function(d) {
  return d.educ;
}));

var simulation = d3.forceSimulation(data)
  .force("x", d3.forceX(function(d) {
    return xScale(d.educ);
  }).strength(1))
  .force("y", d3.forceY(function(d) {
    return d.inlf ? height - 100 : height + 100
  }))
  .force("collide", d3.forceCollide(4))
  .stop();

simulation.tick(120);

graphGroup.append("g")
  .attr("class", "axis axis--x")
  .attr("transform", "translate(0," + height + ")")
  .call(d3.axisBottom(xScale).ticks(20, ".0s"));

var circles = graphGroup.selectAll(null)
  .data(data)
  .enter()
  .append("circle")
  .attr("r", 3)
  .attr("cx", function(d) {
    return d.x;
  })
  .attr("cy", function(d) {
    return d.y;
  });
.cells path {
  fill: none;
  pointer-events: all;
}

.cells :hover circle {
  fill: red;
}
<script src="https://d3js.org/d3.v5.min.js"></script>
Gerardo Furtado
  • 100,839
  • 9
  • 121
  • 171