Published
Edited
Mar 11, 2019
Insert cell
Insert cell
Insert cell
vegalite = require("@observablehq/vega-lite@0.1")
Insert cell
z = require('https://bundle.run/zebras@0.0.11')
Insert cell
Insert cell
TV = d3.csv(TVurl)
Insert cell
bikeshareUrl="https://gist.githubusercontent.com/fanyc14/7e3173a23455866d0c3c20af2c18e9bf/raw/086b1be20b0f3109f298398e22d33c247c9dc877/gistfile1.txt"
Insert cell
bikeShare = d3.csv(bikeshareUrl)
Insert cell
TVParsed={
var TVParse=z.parseNums(["open rate"], TV)
var TVParse2=z.parseNums(["Time"], TVParse)
return TVParse2
}
Insert cell
bikeshareParsed={
var bikeshareParsed=z.parseNums(["meanDaily"], bikeShare)
var bikeshareParsed2=z.parseNums(["lnmeanDaily"], bikeshareParsed)
return bikeshareParsed2
}
Insert cell
bikeParseFilter0={
return z.filter(r=> r.Bike_t=="0", bikeshareParsed)
}
Insert cell
Insert cell
vegalite({
title: "2017 TV viewership of Beijing",
data: {values: TVParsed},
mark: "point",
encoding: {
x: {timeUnit: "month", field: "Date", type: "temporal",
axis: {title: "Date"}}, // this allows us to change the property of axis
y: {aggregate: "mean",field: "Time", type: "quantitative",
scale: {"domain": [100,200]},
axis: {title: "Number of minutes"}} // this allows us to change the property of axis
}
})
Insert cell
viewof view_line_plot = embed({
title: "Subway Ridership by Bike Sharing Intensity",
data: {values: bikeshareParsed},
encoding: {
x: {timeUnit: "yearmonth", field: "Date", type: "temporal",
axis: {title: "Date"}}
},
layer: [
{ mark: {type: "errorband", extent: "ci"},
encoding: {
y: {
field: "meanDaily",type: "quantitative",
scale: {"domain": [50,100]},
axis: {title: "Mean Subway Daily Ridership (95% CI)"}
}
}},
{mark: "line",
encoding: {
y: {
aggregate: "mean",field: "meanDaily", type: "quantitative",
scale: {"domain": [50,100]}}
},
color: {field: "high", type: "nominal"}
},
{mark: {type: "errorband", extent: "ci"},
encoding: {
y: {
field: "meanDaily",type: "quantitative",
scale: {"domain": [50,100]},
axis: {title: "Mean Subway Daily Ridership (95% CI)"}
}
}},
{mark: "line",
encoding: {
y: {
aggregate: "mean",field: "meanDaily", type: "quantitative",
scale: {"domain": [50,100]}}
},
color: {field: "high", type: "nominal"}
}]
})
Insert cell
viewof view_line_plot2 = embed({
title: "Subway Ridership by Bike Sharing Intensity",
data: {values: bikeshareParsed},
mark: {type: "errorband", extent: "stdev", opacity: 0.2},
encoding: {
x: {timeUnit: "yearmonth", field: "Date", type: "temporal",
axis: {title: "Date"}},
y: {
field: "meanDaily", type: "quantitative",scale: {"domain": [40,100]},
title:"Mean of Subway Ridership (95% CIs)"
},
color: {field: "high", type: "nominal"}
}
})
Insert cell
viewof view_2 = embed({
title: "Subway Ridership by Bike Sharing Intensity",
data: {values: bikeParseFilter0},
mark:"line",
encoding:{
x: {timeUnit: "year", field: "Date", type: "temporal", axis: {title: "Date"}},
y:{aggregate: "mean",field: "meanDaily", type: "quantitative",scale: {"domain": [40,100]}},
color: {field: "high", type: "nominal"}
}
})
Insert cell
addMean={
var high_pre=z.filter(r=>r.high=="0",bikeParseFilter0)
var mean_high=z.getCol("meanDaily",high_pre)
var mean_ob_high={"mean": (mean_high.reduce(function(a,b){return a+b},0))/mean_high.length}
var mean_pre_high = high_pre.map(x=> Object.assign(x,mean_ob_high))
var high_post=z.filter(r=>r.high=="0",bikeParseFilter1)
var mean2_high=z.getCol("meanDaily",high_post)
var mean_ob2_high={"mean": (mean2_high.reduce(function(a,b){return a+b},0))/mean2_high.length}
var mean_post_high = high_post.map(x=> Object.assign(x,mean_ob2_high))
return mean_post_high
for (var i=0; i< mean_post_high.length; i++){
mean_pre_high.push(mean_post_high[i])
}
return mean_pre_high
}
Insert cell
Insert cell
viewof view_line_plote = embed({
title: "Subway Ridership by Bike Sharing Intensity",
data: {values: addMean},
layer:[{
mark:"line",
encoding:{
x: {timeUnit: "yearmonth", field: "Date", type: "temporal",
axis: {title: "Date"}},
y:{aggregate: "mean",field: "mean", type: "quantitative"},
color: {field: "high", type: "nominal"}
}
},
{
mark:"line",
encoding:{
x: {timeUnit: "yearmonth", field: "Date", type: "temporal",
axis: {title: "Date"}},
y:{aggregate: "mean",field: "mean", type: "quantitative"},
color: {field: "high", type: "nominal"}
}
},
{
mark: "line",
encoding: {
x: {timeUnit: "yearmonth", field: "Date", type: "temporal",
axis: {title: "Date"}},
y: {
aggregate: "mean",field: "meanDaily", type: "quantitative",scale: {"domain": [40,100]}},
color: {field: "high", type: "nominal"}
}},
//standerror of trend lines
{
mark: {type: "errorband", extent: "stdev", opacity: 0.2},
encoding: {
x: {timeUnit: "yearmonth", field: "Date", type: "temporal",
axis: {title: "Date"}},
y: {
field: "meanDaily", type: "quantitative",scale: {"domain": [40,100]},
title:"Mean of Subway Ridership (95% CIs)"
},
color: {field: "high", type: "nominal"}
}}
]
})
Insert cell
Insert cell
viewof view_box_plot = embed({
title:{text:"2017 TV viewership in Beijing"},
width: 400,
data: {values: TVParsed},
hconcat:[{mark: "point",
encoding: {
x: {timeUnit: "month", field: "Date", type: "temporal",
axis: {title: "Date"}}, // this allows us to change the property of axis
y: {aggregate: "mean",field: "Time", type: "quantitative",
scale: {"domain": [100,200]},
axis: {title: "Number of minutes"}} // this allows us to change the property of axis
}
},
{mark: "point",
encoding: {
x: {timeUnit: "month", field: "Date", type: "temporal",
axis: {title: "Date"}}, // this allows us to change the property of axis
y: {aggregate: "mean",field: "open rate", type: "quantitative",
scale: {"domain": [0.4,0.7]},
axis: {title: "open rate"}} // this allows us to change the property of axis
}}]
})
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