Public
Edited
May 27, 2023
Fork of Plot Map
Insert cell
Insert cell
Insert cell
Insert cell
import {vl} from "@vega/vega-lite-api-v5"
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
statePregnancy
Insert cell
Insert cell
Insert cell
calculatedResult = {

const rate = rate_selected
// Filter NA values
const filteredResult = statePregnancy.filter(item => !isNaN(item[rate]));

// map the state fips code
const stateNames = FileAttachment('state_names.csv').csv();
const mapResult = statePregnancy.map((item) => {
stateNames.then((states) => {
states.forEach((state) => {
if (state['Alpha code'] == item.state) {
item.code = state.Code;
item.fullName = state.State;
}
});
});
return item;
});

// group by state
const groupedResult = mapResult.reduce((result, item) => {
const state = item.state;
if (!result[state]) {
result[state] = [];
}
result[state].push(item);
return result;
}, {});
// calculate the mean rate
let calculatedResult = Object.entries(groupedResult).map(([state, data]) => {
const sum = data.reduce((total, item) => total + parseFloat(item[rate]), 0);
const mean = sum / data.length;
const code = data.length > 0 ? data[0].code : undefined;
return {
code,
state,
mean
};
});

// calculatedResult = calculatedResult.map((d, i) => ({...d, code: id[i], fullName: states[i]}))

return calculatedResult;
}
Insert cell
calculatedResult.map(d => d.code)
Insert cell
calculatedResult.map(d => d.fullName)
Insert cell
id = ["1","2","4","5","6","8","9","10","11","12","13","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","44","45","46","47","48",undefined,"49","50","51","53","54","55","56"];

Insert cell
states = ["AL","AK","AZ","AR","CA","CO","CT","DE","DC","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","US","UT","VT","VA","WA","WV","WI","WY"];
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more