Public
Edited
Jan 19, 2023
Insert cell
Insert cell
import {aq, op} from "@uwdata/arquero"
Insert cell
data = {
const url = "https://raw.githubusercontent.com/mauforonda/aguas-del-rio-pilcomayo/master/data/mediciones_procesadas.csv"
const dateformat = d3.utcParse('%Y-%m-%d %H:%M:%S')
let raw = await aq.loadCSV(url)
raw = raw.derive({
fecha: aq.escape(d => dateformat(d.fecha))
})
return raw.derive({
month: d => op.month(d.fecha),
year: d => op.year(d.fecha)
})
}
Insert cell
dataStacked = {
const columnas_descriptivas = ['fecha', 'month', 'year', 'nombre', 'latitud', 'longitud']
const columnas_mediciones = data.columnNames().slice(12,-2)
const columnas = columnas_descriptivas.concat(columnas_mediciones)
return data.
select(columnas).
fold(columnas_mediciones, {as: ['indicador', 'valor']}).
filter(d => !(d.valor === null))
}
Insert cell
dataStacked.view()
Insert cell
df = dataStacked.filter(aq.escape(d => d.year > 2012 & Object.keys(baseline).includes(d.indicador))).derive({valor: aq.escape(d => d.valor / baseline[d.indicador])}).groupby(['year', 'indicador']).rollup({valor: op.median('valor')}).orderby('year')
Insert cell
df.view()
Insert cell
df.filter(d => !d.valor).view()
Insert cell
{
const baseline = dataStacked.
filter(d => d.year <= 2012).
groupby('indicador').
rollup({baseline: op.mean('valor')}).
objects().
reduce((a, v) => ({...a, [v.indicador]:v.baseline}), {})
const df = dataStacked.
filter(aq.escape(d => d.year > 2012 & Object.keys(baseline).includes(d.indicador))).
derive({valor: aq.escape(d => d.valor / baseline[d.indicador])}).
groupby(['year', 'indicador']).
rollup({valor: op.median('valor')}).
orderby('year')
const plot = Plot.plot({
marginTop: 0,
width: width,
marginLeft: 50,
marginRight: 0,
color: {
scheme: 'RdGy',
reverse: true,
type:'diverging-log',
legend: false,
},
y: {
label: null,
tickSize: 0,
tickFormat: d3.format('.0f')
},
x: {
axis: null,
},
marks: [
Plot.cell(df,{
y: 'year',
x: 'indicador',
fill: 'valor',
title: d => `${d.indicador} en ${d.year}\n${d3.format('.1%')(d.valor)} de la media entre 2007 y 2012`,
inset: 0,
sort: {
x: 'fill',
reduce: 'median',
}
})
]
})
const legend = plot.legend('color', {
ticks: 3,
tickFormat: d => d < 1 ? 'normal' : d == 1 ? '' : 'preocupante',
label: null,
width: width})
return html`<div>${legend}${plot}</div>`
}
Insert cell
plot.legend('color', {ticks: 3, tickFormat: d => d < 1 ? 'normal' : d == 1 ? '' : 'preocupante'})
Insert cell
// Object.assign({}, dataStacked.
// groupby(['year', 'indicador']).rollup({valor: op.median('valor')}).orderby('year').object)
Insert cell
baseline = dataStacked.filter(d => d.year <= 2012).groupby('indicador').rollup({baseline: op.mean('valor')}).objects().reduce((a, v) => ({...a, [v.indicador]:v.baseline}), {})
Insert cell
dataStacked
Insert cell
dataStacked.groupby(['year', 'indicador']).rollup({valor: op.array_agg('valor')}).view()
Insert cell
df.view()
Insert cell
// {
// return Plot.plot({
// height: 3500,
// width: 400,
// color: {
// scheme: 'Magma',
// },
// y: {
// // axis: null
// // type: 'symlog',
// grid: true,
// tickFormat: d => ''
// // domain: [0, 500]
// },
// x: {
// tickFormat: d3.format('.0f')
// },
// marks: [
// Plot.cellX(df, Plot.normalizeY('first', {
// x: 'year',
// y: 'valor',
// fill: 'indicador',
// title: d => `${d.valor}\n${d.indicador}\n${d.year}`,
// inset: 0,
// sort: {
// y: 'fill',
// reverse: true
// }
// }))
// ]
// })
// }
Insert cell
e = 'El Puente'
Insert cell
df.view()
Insert cell
// {
// let dfi = df.filter(aq.escape(d => d.nombre == e)).orderby('year')
// const indicadores = dfi.filter(d => d.year == 2007).array('indicador')
// dfi = dfi.filter(aq.escape( d => indicadores.includes(d.indicador)))
// const positive = Object.fromEntries(dfi.groupby('indicador').rollup({a: op.array_agg("valor")}).objects().map((i) => [i.indicador, i.a.slice(-1)[0] > 1]))
// dfi = dfi.derive({trend: aq.escape(d => positive[d.indicador])})
// // dfi.groupby('indicador').rollup({last: op.last_value('valor') > 0})
// return Plot.plot({
// height: 500,
// width: width,
// y: {
// type: 'symlog',
// grid: true,
// domain: [0, 100]
// },
// x: {
// tickFormat: d3.format('.0f')
// },
// marks: [
// Plot.lineX(dfi, Plot.normalizeY('first', {
// x: 'year',
// y: 'valor',
// stroke: 'indicador',
// title: 'indicador',
// strokeOpacity: .3,
// curve: 'basis',
// // text: 'indicador',
// // textAnchor: 'start',
// // dx: 3
// }))
// ]
// })
// }
Insert cell
Object.fromEntries(dfi.groupby('indicador').rollup({a: op.array_agg("valor")}).objects().map((i) => [i.indicador, i.a.slice(-1)[0] > 1]))
Insert cell
dfi = df.filter(aq.escape(d => d.nombre == e)).orderby('year')
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