Public
Edited
Mar 20, 2023
Insert cell
Insert cell
<svg width="1000" height="1000">
<text x="20" y="35" style="font-size:24px; font-family: sans-serif">U.S. Power Plants</text>
</svg>
Insert cell
Insert cell
powerplants_allUS_2021.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
data = FileAttachment("powerplants_allUS_2021.csv").csv() // parse the CSV file into an Array (list)
Insert cell
toNum = (text) => Number(text.replace(/,/g,'')); // a function to strip commas and make a number
Insert cell
data.forEach(d => {
// iterate through the full dataset and clean two columns that we need.
if (d["Plant annual net generation (MWh)"] != null) {d["Plant annual net generation (MWh)"] = toNum(d["Plant annual net generation (MWh)"])}
if (d["Plant annual CO2 equivalent emissions (tons)"] != null) {d["Plant annual CO2 equivalent emissions (tons)"] = toNum(d["Plant annual CO2 equivalent emissions (tons)"])}
})
Insert cell
Insert cell
Insert cell
viz = d3.select(powerplants) // select the SVG cell as a D3 object called viz
Insert cell
vizWidth = Number(viz.attr("width")) // get width
Insert cell
vizHeight = Number(viz.attr("height")) // get height
Insert cell
baseline = vizHeight - 150 // the 0 data location in Y (negative values below)
Insert cell
Insert cell
circles = viz.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", d => scaleX(d["Plant longitude"]))
.attr("cy", d => scaleY(d["Plant annual CO2 equivalent emissions (tons)"]))
.attr("r", d => rFromArea(d["Plant annual net generation (MWh)"]) * rScale)
.attr("class", d => d["Plant primary fuel category"])
Insert cell
scaleX = d3.scaleLinear()
.domain([-175,-55])
.range([0,vizWidth])
Insert cell
scaleY = d3.scaleLinear()
.domain([0,30000000])
.range([baseline,0])
Insert cell
rFromArea = (area) => Math.sqrt(area/Math.PI);
Insert cell
rScale = 0.03
Insert cell
<style>
circle {
stroke: gray;
fill: yellow;
opacity: 0.6;
}
/* Use style classes to set colors by fuel type */
.COAL {fill:#000000;} /* black */
.BIOMASS {fill:#00ff00;} /* green */
.HYDRO {fill:#0000ff;} /* blue */
.OIL {fill:#777700;} /* brown */
.GAS {fill:#E8BD0C;} /* orange */
.NUCLEAR {fill:#00ffff;} /* cyan */
.GEOTHERMAL {fill:#A6A277;} /* brown */
.WIND {fill: #FF66FF;} /* pink */
.SOLAR {fill: #ffff00;} /* yellow */
.OTHRFOSL {fill: #888888;} /* gray */
.WSTHTOTPUR {fill: #aa0000;} /* gray */
</style>
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