Published
Edited
Feb 13, 2022
1 fork
1 star
Insert cell
# Apple Health Data Analysis and Visualization
Insert cell
## Routine data
Insert cell
### WalkingSpeed
Insert cell
walking_Speed = FileAttachment("google://WalkingSpeed.csv").csv({typed: true})
Insert cell
vl.layer(
vl.markBar({color:'black',width:0.5}),
)
.encode(
vl.x().fieldT('creationDate').timeUnit('monthdate').title('Date'),
vl.y().fieldQ('value').title('km/hr')
)
.data(walking_Speed)
.width(800)
.height(200)
.render()
Insert cell
### Walking Asymmetry Percentage
Insert cell
walking_Asymtry_Percentage = FileAttachment("google://WalkingAsymmetryPercentage.csv").csv({typed: true})
Insert cell
vl.layer(
vl.markBar({color:'black',width:2}),
)
.encode(
vl.x().fieldT('creationDate').timeUnit('monthdate').title('Date'),
vl.y().fieldQ('value').title('%')
)
.data(walking_Asymtry_Percentage)
.width(800)
.height(200)
.render()
Insert cell
## StepCount
Insert cell
stp_ct = FileAttachment("stepCount_clean.csv").csv({typed: true})
Insert cell
vl.layer(
vl.markBar({color:'black',width:1}),
)
.encode(
vl.x().fieldT('startDate').timeUnit('yearmonthdate').title('Date'),
vl.y().average('value').title('Step count')
)
.data(stp_ct)
.width(800)
.height(200)
.render()
Insert cell
vl.layer(
vl.markBar({color:'black',width:1}),
)
.encode(
vl.x().fieldT('startDate').timeUnit('monthdate').title('Date'),
vl.y().average('value').title('Average step count per monthdate')
)
.data(stp_ct)
.width(800)
.height(200)
.render()
Insert cell
vl.layer(
vl.markBar({color:'black',width:20}),
vl.markText({color:'Black',dx: 20, dy: -5, align:'left',size:'10'})
.encode(
vl.text().mean('value').format('.0f')
)
)
.encode(
vl.x().fieldT('startDate').timeUnit('month').title('Date'),
vl.y().average('value').title('Average step count per month')
)
.data(stp_ct)
.width(800)
.height(200)
.render()
Insert cell
vl.layer(
vl.markBar({color:'black',width:40}),
vl.markText({color:'white',dx: 55, dy: 10, align:'left',size:'10'})
.encode(
vl.text().mean('value').format('.0f')
),
vl.markLine({color:'red'}).encode(vl.y().aggregate({average:['value']}))
)
.encode(
vl.x().fieldT('startDate').timeUnit('year').title('Date'),
vl.y().average('value').title('Average step count per year')
)
.data(stp_ct)
.width(800)
.height(200)
.render()
Insert cell
## HeartRate
Insert cell
heartrate_var = FileAttachment("HeartRateVariabilitySDNN.csv").csv({typed:true})
Insert cell
wkheartrate = FileAttachment("WalkingHeartRateAverage.csv").csv()
Insert cell
### HeartRate Variability SDNN average by date
Insert cell
vl.layer(
vl.markBar({color:'black',width:0.5}),
)
.encode(
vl.x().fieldT('startDate').title('Date').timeUnit('monthyeardate'),
vl.y().average('value').title('')
)
.data(heartrate_var)
.width(800)
.height(200)
.render()
Insert cell
rst_heartrate =FileAttachment("RestingHeartRate@1.csv").csv({typed:true})
Insert cell
### Resting heart rate
Insert cell
vl.markBar({color:'black',width:0.5})
.encode(
vl.x().fieldT('startDate').title('Date').timeUnit('monthdate'),
vl.y().fieldQ('value').title('Heart rate')
)
.data(rst_heartrate)
.width(800)
.height(200)
.render()
Insert cell
vl.layer(
vl.markBar({color:'black'}),
vl.markText({color:'white',dx: -10, dy: 10, align:'left',size:'10'})
.encode(
vl.text().average('value').format('.0f')
),
vl.markLine({color:'gold'}).encode(vl.y().average('value'))
)
.encode(
vl.x().fieldO('startDate').timeUnit('day').title('Date').scale({domain:['Mon','Tue','Wed','Thu','Fri','Sat','Sun']}),
vl.y().average('value').title('Heart rate(Bpm)')
)
.title('Resting heart rate')
.data(rst_heartrate)
.width(800)
.height(150)
.render()
Insert cell
### Walking heart rate average
Insert cell
vl.markBar({color:'black',width:0.5})
.encode(
vl.x().fieldT('startDate').title('Date').timeUnit('monthdate'),
vl.y().average('value').title('')
)
.data(wkheartrate)
.width(800)
.height(200)
.render()
Insert cell
vl.layer(
vl.markBar({color:'black'}),
vl.markText({color:'white',dx: -18, dy: 10, align:'left',size:'10'})
.encode(
vl.text().average('value').format('.0f')
),
vl.markLine({color:'gold'}).encode(vl.y().average('value'))
)
.encode(
vl.x().fieldO('startDate').timeUnit('day').title('Date').scale({domain:['Mon','Tue','Wed','Thu','Fri','Sat','Sun']}),
vl.y().average('value').title('Heart rate(Bpm)')
)
.title('Walking heart rate')
.data(wkheartrate)
.width(800)
.height(150)
.render()
Insert cell
### Rest vs Walking heart rate comparison
Insert cell
heartrate = (await Promise.all([ FileAttachment("RestingHeartRate@1.csv"), FileAttachment("HeartRateVariabilitySDNN.csv")]
.map(async file => [file.name.slice(0, -1).toUpperCase(), await file.csv({typed: "true"})])))
.flatMap(([Symbol, data]) => data.map(d => ({Symbol, ...d})))
Insert cell
Plot.plot({
marginRight: 50,
y: {
grid: true,
label: "Heart Rate"
},
x: {
axis: null
},
marks: [
Plot.line(heartrate, {x: "endDate", y: "value", stroke: "Symbol"}),
Plot.text(heartrate, Plot.selectLast({x: "endDate", y: "value", z: "Symbol", text: "Symbol", textAnchor: "start", dx: 1}))
],
width:800
})
Insert cell
## Workout
Insert cell
wkot = FileAttachment("Workout.csv").csv({type:true})
Insert cell
vl.markBar({color:'black',width:3})
.encode(
vl.x().fieldT('startDate').title('Date').timeUnit('yearmonthdate'),
vl.y().fieldQ('duration').title('totalEnergyBurned/Unit:Kcal')
)
.data(wkot)
.width(800)
.height(200)
.render()
Insert cell
# Sleep
Insert cell
slp = FileAttachment("sleep_clean").csv({type:true})
Insert cell
Insert cell
vl.markBar({color:'black'})
.encode(
vl.x().fieldO('endDate').timeUnit('yearmonthdate').title('Date'),
vl.y().average('sleepHour').title('Sleep time(hour)')
)
.data(slp)
.title('Sleep time')
.width(800)
.height(150)
.render()
Insert cell
vl.markBar({color:'black'})
.encode(
vl.x().fieldO('endDate').timeUnit('month').title('Date'),
vl.y().average('sleepHour').title('Sleep time(hour)')
)
.data(slp)
.width(800)
.height(150)
.render()
Insert cell
vl.layer(
vl.markBar({color:'black'}),
vl.markText({color:'white',dx: -15, dy: 10, align:'left',size:'10'})
.encode(
vl.text().average('sleepHour').format('.0f')
),
vl.markLine({color:'red'}).encode(vl.y().aggregate({average:['value']}))
)
.encode(
vl.x().fieldO('endDate').timeUnit('day').title('Date').scale({domain:['Mon','Tue','Wed','Thu','Fri','Sat','Sun']}),
vl.y().average('sleepHour').title('Sleep time(hour)')
)
.data(slp)
.title('Average sleep time per weekday')
.width(800)
.height(150)
.render()
Insert cell
stp_slp_cor = FileAttachment("step_sleep_corr.csv").csv({type:true})
Insert cell
vl.markBar({color:'black'})
.encode(
vl.y().fieldO('value').sort('ascending').title('Step count'),
vl.x().average('sleepHour').title('Sleep Time(hour)')
)
.data(stp_slp_cor)
.width(600)
.height(800)
.render()
Insert cell
Plot.plot({
"data": "url:stp_slp_cor.csv",
"layer": [
{
"mark": {
"type": "point",
"filled": true
},
"encoding": {
"x": {
"field": "value",
"type": "quantitative"
},
"y": {
"field": "sleepHour",
"type": "quantitative"
}
}
},
{
"mark": {
"type": "line",
"color": "firebrick"
},
"transform": [
{
"regression": "value",
"on": "sleepHour"
}
],
"encoding": {
"x": {
"field": "sleepHour",
"type": "quantitative"
},
"y": {
"field": "value",
"type": "quantitative"
}
}
},
{
"transform": [
{
"regression": "value",
"on": "sleepHour",
"params": true
}],
"mark": {
"type": "text",
"color": "firebrick",
"x": "width",
"align": "right",
"y": -5
},
"encoding": {
"text": {"type": "nominal", "field": "R2"}
}
}
]
})
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