Skip to content

Instantly share code, notes, and snippets.

@poezn
Created July 21, 2012 20:31
Show Gist options
  • Save poezn/3157064 to your computer and use it in GitHub Desktop.
Save poezn/3157064 to your computer and use it in GitHub Desktop.
just another inlet to tributary
// showing rate of population who owns firearms (height) vs red/blue state
var data = [{"state": "Alabama","own_firearms": 51.7,"votes_republican": 60.32,"homicide_rate": 10},
{"state": "Alaska","own_firearms": 57.8,"votes_republican": 59.42,"homicide_rate": 4.4},
{"state": "Arizona","own_firearms": 31.1,"votes_republican": 53.39,"homicide_rate": 7.4},
{"state": "Arkansas","own_firearms": 55.3,"votes_republican": 58.72,"homicide_rate": 7.7},
{"state": "California","own_firearms": 21.3,"votes_republican": 36.91,"homicide_rate": 6},
{"state": "Colorado","own_firearms": 34.7,"votes_republican": 44.71,"homicide_rate": 3.8},
{"state": "Connecticut","own_firearms": 16.7,"votes_republican": 38.22,"homicide_rate": 3.8},
{"state": "Delaware","own_firearms": 25.5,"votes_republican": 36.93,"homicide_rate": 7.8},
{"state": "The District","own_firearms": 3.8,"votes_republican": 6.53,"homicide_rate": 25.6},
{"state": "Florida","own_firearms": 24.5,"votes_republican": 48.1,"homicide_rate": 7.5},
{"state": "Georgia","own_firearms": 40.3,"votes_republican": 52.1,"homicide_rate": 7.3},
{"state": "Hawaii","own_firearms": 8.7,"votes_republican": 26.58,"homicide_rate": 2.2},
{"state": "Idaho","own_firearms": 55.3,"votes_republican": 61.21,"homicide_rate": 1.6},
{"state": "Illinois","own_firearms": 20.2,"votes_republican": 36.74,"homicide_rate": 6.7},
{"state": "Indiana","own_firearms": 39.1,"votes_republican": 48.82,"homicide_rate": 5.2},
{"state": "Iowa","own_firearms": 42.8,"votes_republican": 44.39,"homicide_rate": 2.8},
{"state": "Kansas","own_firearms": 42.1,"votes_republican": 56.48,"homicide_rate": 3.9},
{"state": "Kentucky","own_firearms": 47.7,"votes_republican": 57.37,"homicide_rate": 5.7},
{"state": "Louisiana","own_firearms": 44.1,"votes_republican": 58.56,"homicide_rate": 12.2},
{"state": "Maine","own_firearms": 40.5,"votes_republican": 40.38,"homicide_rate": 2.7},
{"state": "Maryland","own_firearms": 21.3,"votes_republican": 36.47,"homicide_rate": 9.3},
{"state": "Massachusetts","own_firearms": 12.6,"votes_republican": 35.99,"homicide_rate": 2.6},
{"state": "Michigan","own_firearms": 38.4,"votes_republican": 40.89,"homicide_rate": 6.5},
{"state": "Minnesota","own_firearms": 41.7,"votes_republican": 43.82,"homicide_rate": 2.5},
{"state": "Mississippi","own_firearms": 55.3,"votes_republican": 56.17,"homicide_rate": 11.3},
{"state": "Missouri","own_firearms": 41.7,"votes_republican": 49.36,"homicide_rate": 8.3},
{"state": "Montana","own_firearms": 57.7,"votes_republican": 49.49,"homicide_rate": 4.2},
{"state": "Nebraska","own_firearms": 38.6,"votes_republican": 56.53,"homicide_rate": 4.4},
{"state": "Nevada","own_firearms": 33.8,"votes_republican": 42.65,"homicide_rate": 6.2},
{"state": "New Hampshire","own_firearms": 30,"votes_republican": 44.52,"homicide_rate": 1.6},
{"state": "New Jersey","own_firearms": 12.3,"votes_republican": 41.61,"homicide_rate": 4.5},
{"state": "New Mexico","own_firearms": 34.8,"votes_republican": 41.78,"homicide_rate": 7.8},
{"state": "New York","own_firearms": 18,"votes_republican": 36.03,"homicide_rate": 4.5},
{"state": "North Carolina","own_firearms": 41.3,"votes_republican": 49.38,"homicide_rate": 7.3},
{"state": "North Dakota","own_firearms": 50.7,"votes_republican": 53.15,"homicide_rate": ""},
{"state": "Ohio","own_firearms": 32.4,"votes_republican": 46.8,"homicide_rate": 5.4},
{"state": "Oklahoma","own_firearms": 42.9,"votes_republican": 65.65,"homicide_rate": 6.5},
{"state": "Oregon","own_firearms": 39.8,"votes_republican": 40.4,"homicide_rate": 2.7},
{"state": "Pennsylvania","own_firearms": 34.7,"votes_republican": 44.15,"homicide_rate": 5.9},
{"state": "Rhode Island","own_firearms": 12.8,"votes_republican": 35.06,"homicide_rate": 2.7},
{"state": "South Carolina","own_firearms": 42.3,"votes_republican": 53.87,"homicide_rate": 8},
{"state": "South Dakota","own_firearms": 56.6,"votes_republican": 53.16,"homicide_rate": 2.5},
{"state": "Tennessee","own_firearms": 43.9,"votes_republican": 56.85,"homicide_rate": 7.8},
{"state": "Texas","own_firearms": 35.9,"votes_republican": 55.39,"homicide_rate": 6.1},
{"state": "Utah","own_firearms": 43.9,"votes_republican": 62.24,"homicide_rate": 1.7},
{"state": "Vermont","own_firearms": 42,"votes_republican": 30.45,"homicide_rate": ""},
{"state": "Virginia","own_firearms": 35.1,"votes_republican": 46.33,"homicide_rate": 4.8},
{"state": "Washington","own_firearms": 33.1,"votes_republican": 40.26,"homicide_rate": 3.4},
{"state": "West Virginia","own_firearms": 55.4,"votes_republican": 55.6,"homicide_rate": 4},
{"state": "Wisconsin","own_firearms": 44.4,"votes_republican": 42.31,"homicide_rate": 2.8},
{"state": "Wyoming","own_firearms": 59.7,"votes_republican": 64.78,"homicide_rate": ""}];
data = _.sortBy(data, function(d){ return -1 * d.own_firearms; });
var own_firearms = _.pluck(data, 'own_firearms')
var yscale = d3.scale.linear()
.domain([d3.min(own_firearms), d3.max(own_firearms)])
.range([0,300]);
var xscale = d3.scale.ordinal()
.domain(own_firearms)
.rangeBands([0, 400]);
var colorscale = d3.scale.linear()
.domain([0, 17])
.range(['white', 'red'])
console.log(own_firearms);
var states = g.selectAll("rect.state")
.data(data)
.enter().append('rect')
.attr('height', function(d, i) {
return yscale(d.own_firearms)
})
.attr('width', xscale.rangeBand())
.attr('y', 0)
.attr('x', function(d, i) {
return xscale(d.own_firearms)
})
.attr('fill', function(d, i) {
if (d.votes_republican > 50) {
return 'red';
} else {
return 'blue';
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment