sigData = FileAttachment("chapter5_sigtests.csv").csv({ typed: true })
sigsDrug = sigData.filter(
({ drug }) => drug === select
)
significance = sigsDrug.filter(({ time }) => time === radio[0].time)
significance
Chapter 5
Adult and Young Adult Demographic Subgroups
Significance Tests
Significance Legend in Graph
sigLegend1 = html`<style>
caption {
font-size: 9px;
}
table {
margin-bottom: 0.5rem;
}
.small td {
font-size: 9px;
color: #000066;
}
</style>
<table class="small" style="width: 250px; ">
<caption style="color: #000066;"> <b><u>2023 Trends</b></u> </caption>
<tr>
<td>Men</td>
<td>1-Year Change</td>
<td>${significance[0]?.oneyrchange}</td>
<td>${significance[0]?.sig_oneyr}</td>
</tr>
<tr>
<td> </td>
<td>5-Year Change</td>
<td>${significance[0]?.fiveyrchange}</td>
<td>${significance[0]?.sig_fiveyr}</td>
</tr>
<tr style="background-color: white;">
<td></td>
<td>10-Year Change</td>
<td>${significance[0]?.tenyrchange}</td>
<td>${significance[0]?.sig_tenyr}</td>
</tr>
<tr>
<td>Women</td>
<td>1-Year Change</td>
<td>${significance[1]?.oneyrchange}</td>
<td>${significance[1]?.sig_oneyr}</td>
</tr>
<tr>
<td> </td>
<td>5-Year Change</td>
<td>${significance[1]?.fiveyrchange}</td>
<td>${significance[1]?.sig_fiveyr}</td>
</tr>
<tr style="background-color: white;">
<td></td>
<td>10-Year Change</td>
<td>${significance[1]?.tenyrchange}</td>
<td>${significance[1]?.sig_tenyr}</td>
</tr>
</table>`
sigLegend2 = html`<style>
caption {
font-size: 9px;
}
table {
margin-bottom: 0.5rem;
}
.small td {
font-size: 9px;
color: #000066;
}
</style>
<table class="small" style="width: 250px; ">
<caption style="color: #000066;"> <b><u>2023 Trends</b></u> </caption>
<tr>
<td>Black</td>
<td>1-Year Change</td>
<td>${significance[3]?.oneyrchange}</td>
<td>${significance[3]?.sig_oneyr}</td>
</tr>
<tr>
<td> </td>
<td>5-Year Change</td>
<td>${significance[3]?.fiveyrchange}</td>
<td>${significance[3]?.sig_fiveyr}</td>
</tr>
<tr style="background-color: white;">
<td></td>
<td>10-Year Change</td>
<td>${significance[3]?.tenyrchange}</td>
<td>${significance[3]?.sig_tenyr}</td>
</tr>
<tr>
<td>Hispanic</td>
<td>1-Year Change</td>
<td>${significance[4]?.oneyrchange}</td>
<td>${significance[4]?.sig_oneyr}</td>
</tr>
<tr>
<td> </td>
<td>5-Year Change</td>
<td>${significance[4]?.fiveyrchange}</td>
<td>${significance[4]?.sig_fiveyr}</td>
</tr>
<tr>
<td></td>
<td>10-Year Change</td>
<td>${significance[4]?.tenyrchange}</td>
<td>${significance[4]?.sig_tenyr}</td>
</tr>
<tr>
<td>White</td>
<td>1-Year Change</td>
<td>${significance[2]?.oneyrchange}</td>
<td>${significance[2]?.sig_oneyr}</td>
</tr>
<tr>
<td> </td>
<td>5-Year Change</td>
<td>${significance[2]?.fiveyrchange}</td>
<td>${significance[2]?.sig_fiveyr}</td>
</tr>
<tr>
<td></td>
<td>10-Year Change</td>
<td>${significance[2]?.tenyrchange}</td>
<td>${significance[2]?.sig_tenyr}</td>
</tr>
</table>`
legendPlace = {if (racedata[0]?.drug == "Any Drug Other Than Cannabis" && racedata[0]?.time == "12 Month" && racedata[0]?.age == "Ages 19-30"){
return "top: 50px; left: 100px;";
} else if (racedata[0]?.drug == "Any Drug Other Than Cannabis" && racedata[0]?.time == "30 Day" && racedata[0]?.age == "Ages 19-30") {
return "top: 60px; left: 85px;";
} else if (racedata[0]?.drug == "Any Drug Other Than Cannabis" && racedata[0]?.time == "30 Day" && racedata[0]?.age == "Ages 35-50") {
return "bottom: 65px; left: 60px;";
} else if (racedata[0]?.drug == "Cigarettes" && racedata[0]?.time == "30 Day") {
return "top: 60px; right: 10px;";
} else {
return "bottom: 65px; right: 50px;";
}
}
Combined Sig Plot with Legend
combosexPlot = html`
<div style="position: relative;">
${sexplot}
<div style="position: absolute; ${legendPlace} border: solid 1px black;">
${sigLegend1}
</div>
</div>`
comboracePlot = html`
<div style="position: relative;">
${raceplot}
<div style="position: absolute; ${legendPlace} border: solid 1px black;">
${sigLegend2}
</div>
</div>`
rawdata = FileAttachment("final_graph5_data.csv").csv({ typed: true })
alphabetical = rawdata.sort((a, b) => {
if (a.drug && b.drug) {
return a.drug.localeCompare(b.drug);
} else {
return 0; // Preserve the order if 'name' is missing
}
});
parser = d3.timeParse("%Y")
format = d3.format(".4")
data = {
const subset = alphabetical.map(
({ year, drug, estimate, age, time, sex, raceEthnicity }) => ({
year: parser(year),
year2: format(year),
estimate: +estimate,
drug: drug,
age: age,
time: time,
sex: sex,
raceEthnicity: raceEthnicity
})
);
const filtered = subset.filter(thing =>
thing.drug?.toLocaleLowerCase() === select?.toLocaleLowerCase()
);
return filtered;
}
Citations
citation1 = html`<div style="max-width: 750px;"><p style="font-size: small;">Suggested citation: Patrick, M. E., Miech, R. A., Johnston, L. D., & O’Malley, P. M. (2024). Monitoring the Future Panel Study annual report: National data on substance use among adults ages 19 to 65, 1976-2023. Monitoring the Future Monograph Series. Ann Arbor: Institute for Social Research, The University of Michigan.</p></div>`
citation2 = html`<div style="max-width: 750px;"><p style="font-size: small;">Suggested citation: Patrick, M. E., Miech, R. A., Johnston, L. D., & O’Malley, P. M. (2024). Monitoring the Future Panel Study annual report: National data on substance use among adults ages 19 to 65, 1976-2023. Monitoring the Future Monograph Series. Ann Arbor: Institute for Social Research, The University of Michigan.</p></div>`
Download Data Button
import {PersistInput} from "@john-guerra/persist-input@latest"
viewof select = PersistInput("drug",
Inputs.select(
rawdata.map((d) => d.drug),
{
value: "Alcohol",
width: 175,
unique: true
}
))
viewof radio = {
const values = d3.group(data, (d) => d.time);
return Inputs.radio(values, {
key: values.has("12 Month")
? "12 Month"
: values.keys().next().value
});
}
viewof radio1 = {
const values = d3.group(radio, (d) => d.age);
return Inputs.radio(values, {
key: values.has("Ages 19-30")
? "Ages 19-30"
: values.keys().next().value
});
}
import {addTooltips} from "@mkfreeman/plot-tooltip"
formatter = d3.timeFormat("%Y")
color1 = d3.scaleOrdinal(
["BLACK", "HISPANIC", "WHITE"],
["#59bbeb", "#6ac4a1", "#cca438"]
)
symbol1 = d3.scaleOrdinal(
["WHITE", "BLACK", "HISPANIC"],
["circle", "square", "triangle"]
)
raceplot = addTooltips(
Plot.plot({
ariaLabel: "Adult and Young Adult Substance Use Line Charts",
ariaDescription:
"Drug Prevalence Chart showing estimates of use over time (1975 to 2022) in 19-30 and 35-50 year olds filtered by drug name and reporting interval. Currently showing ${radio[0].drug}",
width: 900,
height: 700,
marginBottom: 50,
style: {
overflow: "visible",
fontSize: 12
},
symbol: {
domain: new Set(racedata.map((d) => d.raceEthnicity)),
range: [...new Set(racedata.map((d) => d.raceEthnicity))].map(symbol1),
legend: true,
swatchSize: 23
},
color: {
domain: new Set(racedata.map((d) => d.raceEthnicity)),
range: [...new Set(racedata.map((d) => d.raceEthnicity))].map(color1),
},
y: {
label: "Percentage (%)",
labelAnchor: "center"
},
x: {
type: "time",
label: "Years",
anchor: "bottom",
labelAnchor: "center",
domain: [new Date("1987-01-01"), new Date("2024-01-01")]
},
marks: [
Plot.ruleY([0]),
Plot.dot(racedata, {
x: "year",
y: "estimate",
r: 4,
fill: "raceEthnicity",
symbol: "raceEthnicity",
title: (d) => `${d.raceEthnicity} \n ${formatter(d.year)}: ${d.estimate}%`
}),
Plot.line(racedata, {
x: "year",
y: "estimate",
z: (d) => // This creates the line breaks
[
d.raceEthnicity,
d.Flag
].join(),
stroke: "raceEthnicity"})
]
}),
{ fill: "raceEthnicity" }
)
color2 = d3.scaleOrdinal(
["MEN", "WOMEN"],
["#59bbeb", "#6ac4a1"]
)
symbol2 = d3.scaleOrdinal(
["MEN", "WOMEN"],
["circle", "square"]
)
sexplot = addTooltips(
Plot.plot({
ariaLabel: "Adult and Young Adult Substance Use Line Charts",
ariaDescription:
"Drug Prevalence Chart showing estimates of use over time (1975 to 2022) in 19-30 and 35-50 year olds filtered by drug name and reporting interval. Currently showing ${radio[0].drug}",
width: 900,
height: 700,
marginBottom: 50,
style: {
overflow: "visible",
fontSize: 12
},
symbol: {
domain: new Set(sexdata.map((d) => d.sex)),
range: [...new Set(sexdata.map((d) => d.sex))].map(symbol2),
legend: true,
swatchSize: 23
},
color: {
domain: new Set(sexdata.map((d) => d.sex)),
range: [...new Set(sexdata.map((d) => d.sex))].map(color2),
},
y: {
label: "Percentage (%)",
labelAnchor: "center"
},
x: {
type: "time",
label: "Years",
anchor: "bottom",
labelAnchor: "center",
domain: [new Date("1987-01-01"), new Date("2024-01-01")]
},
marks: [
Plot.ruleY([0]),
Plot.dot(sexdata, {
x: "year",
y: "estimate",
r: 4,
fill: "sex",
symbol: "sex",
title: (d) => `${d.sex} \n ${formatter(d.year)}: ${d.estimate}%`
}),
Plot.line(sexdata, {
x: "year",
y: "estimate",
z: (d) => // This creates the line breaks
[
d.sex,
d.Flag
].join(),
stroke: "sex"})
]
}),
{ fill: "sex" }
)
downloadButton = (data, filename) => {
let downloadData;
downloadData = new Blob([d3.csvFormat(data)], { type: "text/csv" });
const size = (downloadData.size / 1024).toFixed(0);
const button = DOM.download(
downloadData,
filename,
`Download ${filename} Dataset (~${size} KB)`
);
return button;
}
name = `${radio[0].drug} - ${radio[0].time}`
data2 = radio.map(d => ({...d, year: d.year2}))
data3 = data2.map(({year2, ...keep}) => keep)
downloadData1 = downloadButton(data3, name)
downloadData2 = downloadButton(data3, name)