Published
Edited
Nov 9, 2019
1 fork
Insert cell
md`# 355 parks data`
Insert cell
d3 = require('d3@5')
Insert cell
VegaLite = require('vega-embed@5')
Insert cell
md`We are loading the gas prices here and the only 2 important fields are the year and the current gas prices so we decided to return them`
Insert cell
gasPrice = d3.csv("https://www.sfu.ca/~ridgez/ParkData/gas_price.txt", d => {
return {
year: d.year,
price: d.gas_current,
}
})
Insert cell
md`We are loading the relative income data here here and the only 2 important fields are the year and the annual income. Since a date format is given, we have decided to split the date and only retrieve the year.`
Insert cell
income = d3.csv("https://www.sfu.ca/~ridgez/ParkData/MEHOINUSA672N.txt", d => {
return {
year: d.DATE.split("/")[2],
income: +d["Income (2018 Adjusted Dollars)"]
}
})
Insert cell
md`We are loading the park data here and returning the year, state, region, type of park, the name of the park and the number of visitors.`
Insert cell
park = d3.csv("https://www.sfu.ca/~ridgez/ParkData/national_parks.txt", d => {
return {
year: d.year,
state: d.state,
region:d.region,
type: d.unit_type,
name:d.parkname,
visitors: d.visitors,
}
})
Insert cell
md`We are loading the population per state here and returning the year, the state, and the population.`
Insert cell
population = d3.csv("https://www.sfu.ca/~ridgez/ParkData/state_pop.txt", d => {
return {
year: d.year,
state: d.state,
pop: d.pop === "NA" ? 0 : d.pop,
}
})
Insert cell
md`We are loading inbound tourism data from the world to the united states and we are returning the year, country, and the population.`
Insert cell
tourismIn = d3.csv("https://www.sfu.ca/~ridgez/ParkData/TOURISM_INBOUND_09112019011654690.txt", d => {
if (d["VARIABLE"].includes("INB_")) return undefined;
return {
year: d.YEAR,
country: d.Variable,
population: d.Value,
}
})
Insert cell
md`Using the same data as the previous set, we are returning the type of accommodataions that the tourism are staying in.`
Insert cell
tourismAccommodation = d3.csv("https://www.sfu.ca/~ridgez/ParkData/TOURISM_INBOUND_09112019011654690.txt", d => {
if (!d["VARIABLE"].includes("INB_")) return undefined;
return {
year: d.YEAR,
accommodation: d.Variable,
population: d.Value,
}
})
Insert cell
VegaLite({
data: { values: tourismIn },
width: 1000,
height: 300,
mark: { type: "bar"},
encoding: {
x: {
field: "country",
type: "nominal",
},
y: {
field: "population", type: "quantitative",
},
}
})
Insert cell
VegaLite({
data: { values: tourismAccommodation },
width: 1000,
height: 300,
mark: { type: "bar"},
encoding: {
x: {
field: "accommodation",
type: "nominal",
},
y: {
field: "population", type: "quantitative",
},
}
})
Insert cell
VegaLite({
data: {values: gasPrice},
mark: "line",
encoding: {
x: {field: "year", type: "temporal", sort: {encoding: "x", order: "assending"}},
y: {field: "price", type: "quantitative"},
}
})
Insert cell
{
let numOfLayers = 20;

// let maxPop = d3.max(population.map(d => d.pop));
let maxPop = 40000000;

let perLayerRange = maxPop / numOfLayers;

let layers = [];
let templateLayer = {
width: 300,
height: 50,
mark: { type: "area", line: true, clip: true, orient: "vertical" },
transform: [
{
calculate: "datum.pop - 50",
as: "popShifted"
}
],
encoding: {
x: {
field: "year",
type: "temporal",
scale: {
zero: false,
nice: false
},
axis: {
ticks: false,
title: null,
}
},
y: {
field: "popShifted", type: "quantitative",
scale: { domain: [0, 1000000] }
},
opacity: { value: .1 }
}
};

for (let i = 0; i < numOfLayers; i++) {
let newLayer = JSON.parse(JSON.stringify(templateLayer));
newLayer.encoding.y.scale.domain[0] = 0;
newLayer.encoding.y.scale.domain[1] = perLayerRange;
newLayer.transform[0].calculate = "datum.pop - " + perLayerRange * i;
layers.push(newLayer);
}
return VegaLite({
data: { values: population },
facet: {
row: {
field: "state",
type: "nominal",
}


},
spec: {
layer: layers
}

})
}
Insert cell
VegaLite({
data: { values: population },
width: 300,
height: 300,
mark: { type: "circle"},
encoding: {
x: {
field: "year",
type: "temporal",
},
y: {
field: "pop", type: "quantitative",
},
}
})
Insert cell
VegaLite({
data: { values: income },
width: 300,
height: 300,
mark: { type: "line"},
encoding: {
x: {
field: "year",
type: "temporal",
},
y: {
field: "income", type: "quantitative",
},
}
})
Insert cell
md`Sources`
Insert cell
md`https://data.world/inform8n/us-national-parks-visitation-1904-2016-with-boundaries
https://github.com/rfordatascience/tidytuesday/tree/master/data/2019/2019-09-17
https://stats.oecd.org/Index.aspx?DataSetCode=TOURISM_INBOUND
https://fred.stlouisfed.org/series/MEHOINUSA672N
`
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more