Public
Edited
Dec 21, 2023
Fork of proj
Insert cell
Insert cell
Insert cell
death-rate-amphetamine@2.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
death-rate-from-opioid-use@1.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
death-rates-cocaine@1.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
death-rates-from-drug-use-disorders@1.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
deaths-drug-overdoses@1.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
deaths-due-to-illicit-drug-use-by-age@1.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
deaths-from-drug-use-disorders-by-age@1.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
deaths-illicit-drugs@1.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
drug-disorder-dalys-rate@1.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
drug-overdose-death-rates@1.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
mental-health-as-risk-for-drug-dependency@1.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
prevalence-of-drug-use-disorders-by-age@1.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
prevalence-of-drug-use-disorders-males-vs-females@1.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
share-with-drug-use-disorders@1.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
Insert cell
viewof country01 = Inputs.select(countries, {label: "Выбрать страну"})
Insert cell
viewof year01 = Inputs.range([minYear, maxYear], {label: "Год", step: 1})
Insert cell
chart = PieChart(transformedDeathSums, {
name: d => d.name,
value: d => d.value,
width,
height: 500
})
Insert cell
Insert cell
viewof country02 = Inputs.select(countries, {label: "Выбрать страну"})
Insert cell
viewof year02 = Inputs.range([minYear, maxYear], {label: "Год", step: 1})
Insert cell
Plot.plot({
marks: [
Plot.barY(chartData, {
x: "Возрастная группа",
y: "%",
sort: {x: "y", reverse: true},
fill: d => d.name
}),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
viewof country03 = Inputs.select(countries, {label: "Выбрать страну"})
Insert cell
Plot.plot({
marks: [
Plot.ruleY([0]),
Plot.lineY(transformedData3, {x: "year", y: "%", stroke: "name"}),
Plot.dot(transformedData3, {x: "year", y: "%", stroke: "name", fill: "name"}),
],
legend: {orient: "top-right"}
})
Insert cell
Insert cell
viewof year03 = Inputs.range([minYear, maxYear], {label: "Год", step: 1})
Insert cell
Plot.plot({
marks: [
Plot.barX(deathSums6, {x: "death", y: "Entity", fill:"Entity", sort: {y: "x", reverse: true}}),
Plot.ruleX([0,])
],
color: { legend: true },
marginLeft: 200,
width:1000,
height:3000
})
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.barX(mentalHealthAsRiskForDrugDependency1, {x: "Increased risk of illicit drug dependency", y: "Entity", fill:"Increased risk of illicit drug dependency", sort: {x: "x", reverse: true}}),
Plot.ruleX([0,])
],
color: { legend: true },
marginLeft: 200
})
Insert cell
Insert cell
viewof year7 = Inputs.range([minYear, maxYear], {label: "Год", step: 1})
Insert cell
viewof country7 = Inputs.select(countries, {label: "Выбрать страну"})
Insert cell
Plot.plot({
y: {
label: "Increased risk of illicit drug dependency"
},
x: {
label: "Category"
},
marks: [
Plot.barY(prevalenceOfDrugUseDisordersMalesVsFemales3, {
y: "Value",
x: "Category",
fill: "Category"
})
]
})
Insert cell
import {PieChart} from "@d3/pie-chart"
Insert cell
transformedDeathSums = Object.keys(deathSums).map(key => {
if (key !== 'Year') {
return {
Year: deathSums.Year,
name: nameMap[key],
value: deathSums[key]
};
}
}).filter(Boolean);
Insert cell
data731 = deathRateAmphetamine2.concat(deathRateFromOpioidUse1, deathRatesCocaine1, deathRatesFromDrugUseDisorders1);
Insert cell
minYear = deathByDrug.reduce((min, row) => Math.min(min, row.Year), deathByDrug[0].Year);
Insert cell
deathByDrug1 = data731.map( function (i) {
return {
Entity : i['Entity'],
Year : i['Year'],
Deaths_Amphetamine : i['Deaths - Amphetamine use disorders - Sex: Both - Age: Age-standardized (Rate)'],
Deaths_Opioid : i['Deaths - Opioid use disorders - Sex: Both - Age: Age-standardized (Rate)'],
Deaths_Cocaine : i['Deaths - Cocaine use disorders - Sex: Both - Age: Age-standardized (Rate)'],
DrugRelatedDeaths : i['Deaths - Drug use disorders - Sex: Both - Age: Age-standardized (Rate)'],
}})
Insert cell
deathByDrug3 = deathByDrug1.reduce(function(acc, cur) {
var key = cur.Entity + '-' + cur.Year;
if (!acc[key]) {
acc[key] = {
Entity: cur.Entity,
Year: cur.Year,
Deaths_Amphetamine: 0,
Deaths_Opioid: 0,
Deaths_Cocaine: 0,
DrugRelatedDeaths: 0
};
}
acc[key].Deaths_Amphetamine += isNaN(cur.Deaths_Amphetamine) ? 0 : cur.Deaths_Amphetamine;
acc[key].Deaths_Opioid += isNaN(cur.Deaths_Opioid) ? 0 : cur.Deaths_Opioid;
acc[key].Deaths_Cocaine += isNaN(cur.Deaths_Cocaine) ? 0 : cur.Deaths_Cocaine;
acc[key].DrugRelatedDeaths += isNaN(cur.DrugRelatedDeaths) ? 0 : cur.DrugRelatedDeaths;
return acc;
}, {});
Insert cell
deathByDrug = Object.values(deathByDrug3);
Insert cell
Inputs.table(deathByDrug)
Insert cell
maxYear = deathByDrug.reduce((max, row) => Math.max(max, row.Year), deathByDrug[0].Year);
Insert cell
Insert cell
countries.push("World");
Insert cell
data2019 = deathByDrug.filter(d => d.Year === year01 && (d.Entity === country01 || country01 === "World"));
Insert cell
deathSums = data2019.map(d => ({
Year: d.Year,
Deaths_Amphetamine: d3.mean(data2019, d => d.Deaths_Amphetamine),
Deaths_Opioid: d3.mean(data2019, d => d.Deaths_Opioid),
Deaths_Cocaine: d3.mean(data2019, d => d.Deaths_Cocaine),
DrugRelatedDeaths: d3.mean(data2019, d => d.DrugRelatedDeaths)
}))[0];
Insert cell
nameMap = ({
"Deaths_Amphetamine": "Смерти от амфетамина",
"Deaths_Opioid": "Смерти от опиоидов",
"Deaths_Cocaine": "Смерти от кокаина",
"DrugRelatedDeaths": "Смерти Связанные с Наркотиками"
});
Insert cell
prevalenceOfDrugUseDisordersByAge = prevalenceOfDrugUseDisordersByAge1.map( function (i) {
return {
Entity : i['Entity'],
Year : i['Year'],
age5_14 : i['Prevalence - Drug use disorders - Sex: Male - Age: 5-14 years (Percent)'],
age10_14 : i['Prevalence - Drug use disorders - Sex: Both - Age: 10 to 14 (Percent)'],
age15_19 : i['Prevalence - Drug use disorders - Sex: Both - Age: 15 to 19 (Percent)'],
age20_24 : i['Prevalence - Drug use disorders - Sex: Both - Age: 20 to 24 (Percent)'],
age25_29 : i['Prevalence - Drug use disorders - Sex: Both - Age: 25 to 29 (Percent)'],
age30_34 : i['Prevalence - Drug use disorders - Sex: Both - Age: 30 to 34 (Percent)'],
age70 : i['Prevalence - Drug use disorders - Sex: Both - Age: 70+ years (Percent)'],
age15_49 : i['Prevalence - Drug use disorders - Sex: Both - Age: 15-49 years (Percent)'],
}})
Insert cell
data2chart = prevalenceOfDrugUseDisordersByAge.filter(d => d.Year === year02 && (d.Entity === country02 || country02 === "World"));
Insert cell
nameMap2 = ({
"age5_14": "5-14",
"age10_14": "10-14",
"age15_19": "15-19",
"age20_24": "20-24",
"age25_29": "25-29",
"age30_34": "30-34",
"age70": "70+",
"age15_49": "15-49"
});
Insert cell
chartData = Object.keys( data2chart[0]).map(key => {
if (key.startsWith("age")) {
return {
name: nameMap2[key],
"Возрастная группа": nameMap2[key],
"%": data2chart[0][key]
};
}
}).filter(Boolean); // удаляем undefined элементы
Insert cell
data3 = prevalenceOfDrugUseDisordersByAge.filter(d => (d.Entity === country03 || country03 === "World"));
Insert cell
transformedData3 = data3.flatMap(item => {
return Object.entries(item).map(([key, value]) => {
if (key.startsWith("age")) {
return {
year: item.Year,
name: nameMap2[key],
"Возрастная группа": nameMap2[key],
"%": value
};
}
}).filter(Boolean); // удаляем undefined элементы
});

Insert cell
data5 = deathByDrug.filter(d => d.Year === year03);
Insert cell
groupedData6 = d3.group(data5, d => d.Entity);
Insert cell
deathSums6 = Array.from(groupedData6, ([Entity, data]) => ({
Entity: Entity,
death: d3.mean(data, d => d.Deaths_Amphetamine + d.Deaths_Opioid + d.Deaths_Cocaine + d.DrugRelatedDeaths)/100
}));

Insert cell
prevalenceOfDrugUseDisordersMalesVsFemales = prevalenceOfDrugUseDisordersMalesVsFemales1.filter(d => d.Year === year7 && (d.Entity === country7 || country7 === "World"));
Insert cell
prevalenceOfDrugUseDisordersMalesVsFemales3 = prevalenceOfDrugUseDisordersMalesVsFemales.flatMap(item => [
{Category: "Male", Value: item["Prevalence - Drug use disorders - Sex: Male - Age: Age-standardized (Percent)"]},
{Category: "Female", Value: item["Prevalence - Drug use disorders - Sex: Female - Age: Age-standardized (Percent)"]}
]);
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