Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save clairepapu/7cc280d4978a7ba1a1d4 to your computer and use it in GitHub Desktop.
Save clairepapu/7cc280d4978a7ba1a1d4 to your computer and use it in GitHub Desktop.
Availability of your favorite fruits over the years (US, 1970-2012)
Fruit 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
Apples 17.23 16.62 15.68 16.28 16.56 19.68 17.24 16.69 18.14 17.34 19.41 17.01 17.7 18.43 18.52 17.42 18 21.02 20.02 21.43 19.81 18.33 19.37 19.22 19.58 18.89 18.88 18.28 19.19 18.71 17.64 15.76 16.16 17.06 18.97 16.81 17.9 16.55 16.05 16.39 15.43 15.53 16.1
Bananas 17.38 18.06 17.92 18.16 18.49 17.64 19.25 19.21 20.19 20.98 20.77 21.48 22.54 21.25 22.18 23.48 25.82 25.02 24.29 24.71 24.34 25.03 27.1 26.58 27.76 27.06 27.58 27.14 28 30.69 28.45 26.63 26.78 26.17 25.78 25.18 25.11 25.95 25.04 22.01 25.61 25.54 25.4
Oranges 16.16 15.73 14.47 14.43 14.42 15.88 14.74 13.44 13.44 11.5 14.28 12.36 11.69 15.04 11.87 11.6 13.43 12.81 13.9 12.17 12.36 8.43 12.83 14.13 12.93 11.82 12.57 13.91 14.6 8.37 11.74 11.88 11.74 11.9 10.8 11.42 10.25 7.46 9.93 9.06 9.68 9.96 10.7
Grapes 2.92 2.56 2.54 2.91 3.17 3.65 3.57 3.58 3.12 3.49 4.01 4.09 5.77 5.64 6.14 6.91 7.16 7.11 7.77 8.02 7.91 7.35 7.21 7.06 7.11 7.54 6.8 7.85 7.25 8.06 7.52 7.45 8.5 7.73 7.87 8.68 7.67 8.09 8.6 7.99 8.24 7.66 7.87
Tangerines 2.24 2.34 2.06 2.07 2.23 2.56 2.36 2.63 2.08 2 2.2 2.03 2.07 2.25 2.07 1.51 1.59 1.78 1.77 1.71 1.31 1.38 1.93 1.86 2.09 1.99 2.15 2.52 2.17 2.3 2.86 2.72 2.56 2.72 2.77 2.5 2.69 2.56 3.08 3.16 3.77 3.97 3.99
Grapefruit 8.22 8.55 8.56 8.57 8.21 8.36 9.26 7.73 8.35 7.29 7.3 6.65 7.2 7.84 5.98 5.51 6.13 6.35 6.69 6.6 4.43 5.86 5.92 6.19 6.07 5.99 5.84 6.18 5.93 5.75 5.09 4.85 4.63 4.1 4.14 2.65 2.31 2.84 3.16 2.8 2.76 2.71 2.37
Lemons 2.06 2.25 1.87 1.94 2.01 1.95 1.89 2.11 2.13 1.91 1.92 2.01 2.07 2.32 2.16 2.3 2.47 2.48 2.47 2.39 2.6 2.6 2.53 2.63 2.65 2.83 2.86 2.76 2.46 2.61 2.44 2.96 3.34 3.33 3.12 2.95 4.15 2.81 1.97 3.12 2.66 3.32 3.82
Limes 0.17 0.17 0.21 0.21 0.2 0.22 0.25 0.23 0.22 0.27 0.36 0.42 0.38 0.52 0.45 0.56 0.58 0.51 0.56 0.69 0.66 0.75 1.02 0.95 0.97 1.18 1.14 1.15 1.4 1.33 1.39 1.5 1.1 1.77 1.85 2.09 2.26 2.27 2.48 2.55 2.57 2.5 2.56
Apricots 0.12 0.13 0.08 0.09 0.06 0.08 0.1 0.09 0.07 0.08 0.1 0.1 0.08 0.08 0.13 0.16 0.1 0.08 0.16 0.09 0.16 0.13 0.15 0.13 0.15 0.1 0.09 0.14 0.12 0.12 0.15 0.08 0.09 0.13 0.12 0.13 0.08 0.16 0.13 0.14 0.12 0.12 0.1
Avocados 0.45 0.83 0.43 0.71 1.25 0.73 1.19 1.13 1.27 0.85 2.1 1.56 1.86 2.21 1.84 1.52 2.37 1.6 1.58 1.09 1.43 1.45 2.19 1.34 1.37 1.59 1.6 1.75 1.54 1.94 2.23 2.53 2.36 2.77 3.09 3.51 3.63 3.53 3.84 4.28 4.08 5.14 5.34
Cantaloupe 7.01 6.64 6.77 5.88 5.14 5.05 5.05 5.52 6.58 6.12 5.85 6.12 7.67 6.54 7.67 8.47 9.43 9.15 7.86 10.36 9.23 8.64 8.47 8.61 8.38 8.97 10.28 10.52 10.64 11.38 11.12 11.16 11.1 10.79 9.81 9.58 9.25 9.58 8.87 9.12 8.55 8.71 7.59
Cherries 0.5 0.67 0.38 0.73 0.58 0.69 0.82 0.63 0.53 0.67 0.69 0.53 0.52 0.73 0.7 0.42 0.49 0.72 0.52 0.54 0.39 0.4 0.53 0.43 0.52 0.29 0.4 0.6 0.52 0.63 0.6 0.77 0.7 0.92 0.99 0.87 1.06 1.22 1 1.56 1.31 1.3 1.5
Cranberries 0.18 0.19 0.15 0.19 0.15 0.14 0.19 0.18 0.18 0.13 0.14 0.21 0.21 0.14 0.13 0.13 0.14 0.11 0.07 1.39 1.32 1.61 1.57 1.42 1.77 1.57 1.67 1.9 1.96 2.24 1.95 1.67 2.02 2.18 2.17 2.14 2.33 2.22 2.56 2.27 2.19 2.45 2.67
Honeydew 0.91 0.93 1.04 1.11 1.01 1.06 1.02 1.13 1.55 1.59 1.42 1.54 1.83 1.77 1.82 2.09 2.43 2.19 2.35 2.49 2.06 1.9 2.1 1.74 2 1.89 2.04 2.22 2.34 2.45 2.26 1.97 2.19 2.17 2.05 1.87 1.88 1.83 1.69 1.6 1.64 1.58 1.54
Mangoes 0.07 0.09 0.09 0.11 0.13 0.16 0.19 0.15 0.21 0.21 0.25 0.2 0.29 0.43 0.43 0.43 0.49 0.56 0.38 0.51 0.54 0.85 0.67 0.89 0.97 1.12 1.34 1.43 1.49 1.62 1.75 1.79 1.97 2.06 2.02 1.88 2.1 2.1 2.1 2.02 2.24 2.53 2.49
Papayas 0.12 0.1 0.11 0.14 0.16 0.16 0.2 0.25 0.25 0.17 0.21 0.22 0.15 0.18 0.26 0.18 0.18 0.19 0.16 0.14 0.18 0.17 0.24 0.28 0.3 0.37 0.54 0.47 0.47 0.62 0.68 0.78 0.79 0.87 1.03 0.94 1.04 1.08 0.98 1.2 1.17 1.05 0.97
Peaches 5.82 5.66 3.88 4.26 4.34 4.98 5.14 5.09 6.09 6.66 7.08 6.86 5.34 5.42 6.7 5.49 5.83 6.05 6.75 5.86 5.54 6.41 5.99 5.84 5.42 5.32 4.38 5.51 4.69 5.28 5.3 5.16 5.23 5.17 5.15 4.83 4.58 4.46 5.08 4.41 4.73 4.5 3.92
Pears 1.92 2.57 2.31 2.59 2.51 2.77 2.85 2.39 2.31 2.31 2.63 2.84 2.87 3.01 2.56 2.8 2.99 3.53 3.25 3.23 3.26 3.18 3.16 3.39 3.48 3.39 3.09 3.43 3.47 3.57 3.43 3.28 3.09 3.11 2.98 2.94 3.22 3.12 3.14 3.21 2.93 3.24 2.82
Pineapples 0.7 0.64 0.77 0.91 0.9 1.03 1.14 1.36 1.43 1.45 1.49 1.54 1.65 1.68 1.5 1.47 1.72 1.62 1.76 1.96 2.05 1.91 1.99 2.04 2.02 1.91 1.89 2.34 2.75 3.03 3.22 3.16 3.82 4.39 4.43 4.9 5.2 5.02 5.07 5.09 5.7 5.72 6.42
Plums 1.47 1.28 1.08 1.14 1.5 1.33 1.25 1.54 1.53 1.61 1.53 1.69 1.06 1.41 1.83 1.42 1.28 1.9 1.72 1.41 1.54 1.42 1.77 1.27 1.6 0.93 1.43 1.51 1.18 1.27 1.19 1.33 1.26 1.24 1.12 1.11 1.02 1.01 0.92 0.73 0.78 0.87 0.62
Strawberries 1.73 1.83 1.67 1.58 1.83 1.8 1.66 1.91 2.12 1.9 1.97 2.17 2.37 2.32 2.96 2.99 2.89 3.11 3.33 3.25 3.26 3.59 3.62 3.64 4.08 4.12 4.35 4.14 3.96 4.59 4.9 4.24 4.68 5.33 5.53 5.85 6.17 6.3 6.51 7.22 7.29 7.39 7.84
Watermelon 13.49 13.04 12.31 12.74 11.32 11.44 12.63 12.62 11.89 11.4 10.65 11.65 12.48 11.31 14.42 13.5 12.75 12.98 13.55 13.62 13.28 12.73 14.7 14.19 15 15.16 16.59 15.49 14.34 15.21 13.82 15.01 14.04 13.54 12.99 13.55 15.12 14.41 15.59 14.92 15.71 14.17 14.8
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Fruits Availability Per Capita in the US, 1970-2012</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: white;
font-family: Helvetica, Arial, sans-serif;
}
h1 {
font-family: Cambria, Georgia, serif; font-size: 24px;
margin-left: 100px; color: dimgray;
}
h2 {
font-family: Helvetica, Ariel, sans-serif; font-size: 14px;
margin-left: 100px; color: dimgray;
}
blockquote {margin-left: 110px;}
p {
font-size: 10px;
color: dimgray;
margin: 10px 0 0 0;
}
svg {
background-color: white;
}
circle:hover {
fill: blue;
}
path {
stroke: gray;
stroke-width: 0.5;
}
g.highlight path {
stroke: gray;
stroke-width: 4;
}
path:hover {
stroke: #ff4444;
stroke-width: 4;
}
.axis path,
.axis line {
fill: none;
stroke: gray;
stroke-width: 1;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
text.label {
font-size: 12px;
}
</style>
</head>
<body>
<h1>Availability of Your Favorite Fruits Over the Years</h1>
<h2>Fruits availablity in the United States, pounds per capita by type, 1970-2012</h2>
<script type="text/javascript">
//Dimensions and padding
var w = 1100;
var h = 800;
var padding = [ 20, 10, 50, 100 ]; //Top, right, bottom, left
//Set up date formatting and years
var dateFormat = d3.time.format("%Y");
//Set up scales
var xScale = d3.time.scale()
.range([ padding[3], w - padding[1] - padding[3] ]);
var yScale = d3.scale.linear()
.range([ padding[0], h - padding[2] ]);
//Configure axis generators
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(30)
.tickFormat(function(d) {
return dateFormat(d);
});
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
//Configure line generator
var line = d3.svg.line()
.x(function(d) {
return xScale(dateFormat.parse(d.year));
})
.y(function(d) {
return yScale(+d.amount);
});
//Create the empty SVG image
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
//Load data
d3.csv("fruits-availablity-pounds-per-capita-by-year-transpose.csv", function(data) {
var years = ["1970", "1971", "1972", "1973", "1974", "1975", "1976", "1977", "1978", "1979", "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012"];
//Create a new, empty array to hold our restructured dataset
var dataset = [];
//Loop once for each row in data
for (var i = 0; i < data.length; i++) {
//Create new object with this country's name and empty array
dataset[i] = {
fruit: data[i].Fruit,
pounds: []
};
//Loop through all the years
for (var j = 0; j < years.length; j++) {
// If value is not empty
if (data[i][years[j]]) {
//Add a new object to the emissions data array
//for this country
dataset[i].pounds.push({
year: years[j],
amount: data[i][years[j]]
});
}
}
}
//Uncomment to log the original data to the console
// console.log(data);
//Uncomment to log the newly restructured dataset to the console
// console.log(dataset);
//Set scale domains
xScale.domain([
d3.min(years, function(d) {
return dateFormat.parse(d);
}),
d3.max(years, function(d) {
return dateFormat.parse(d);
})
]);
yScale.domain([
d3.max(dataset, function(d) {
return d3.max(d.pounds, function(d) {
return +d.amount;
});
}),
0
]);
//Make a group for each country
var groups = svg.selectAll("g")
.data(dataset)
.enter()
.append("g");
//Append a title with the country name (so we get easy tooltips)
groups.append("title")
.text(function(d) {
return d.Fruit + "hello";
});
//Within each group, create a new line/path,
//binding just the emissions data to each one
groups.selectAll("path")
.data(function(d) {
return [ d.pounds ];
})
.enter()
.append("path")
.attr("class", "line")
.attr("d", line)
.attr("fill", "none")
.attr("stroke-width", 2);
//Axes
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + (padding[3]) + ",0)")
.call(yAxis);
});
//End data load function
</script>
<blockquote><p>Source: Calculated by ERS/USDA based on data from various sources (see http://www.ers.usda.gov/data-products/food-availability-(per-capita)-data-system/food-availability-documentation.aspx). Data last updated Feb. 1, 2014.</p></blockquote>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment