d3.legend.js searches for a data-legend-pos attribute before sorting them by getBBox.y, so I added that attribute to each path.