Published
Edited
Mar 31, 2019
Insert cell
Insert cell
Insert cell
Insert cell
// Load a file locally
viewof text = html`<input type=file * ">`
Insert cell
Insert cell
tweetsDF = Object.values(JSON.parse(tweetsStrings))
Insert cell
Insert cell
{
return z.unique(z.getCol("location", tweetsDF))
}
Insert cell
// Variation of Boston: Boston, MA; Boston; boston, etc.
newTweets = {
let newLocations = []
let locations = z.getCol("location", tweetsDF)
for (let id in locations) {
if (locations[id].toLowerCase().includes('boston')){
newLocations.push('Boston, MA')
} else {
newLocations.push(locations[id])
}
}
return z.addCol("cleanLocations", newLocations, tweetsDF)
}
Insert cell
locationCount={
let count= z.filter(r => r.group!= "", z.gbCount("group", z.groupBy(d => d.cleanLocations, newTweets)))
return z.sortByCol('count', 'des', count)
}
Insert cell
vegalite({
data: {values:locationCount.slice(0,25)},
mark: "bar",
encoding: {
x: {field: "group", type: "nominal",sort:{encoding:"y"},axis: {title: "Locations"}},
y: {field: "count", type: "quantitative", axis: {title: "count"}},
color:{field: "group", "type": "nominal"}
}
})
Insert cell
Insert cell
geolocated={
return z.filter(r => r.lon!= null, z.filter(r => r.lat!= null, tweetsDF))
}
Insert cell
vegalite({
data: {values: geolocated},
mark: "point",
encoding: {
x: {field: "lon", type: "quantitative",scale: {domain: [-71.5,-70.5]},axis: {title: "longitude"}},
y: {field: "lat", type: "quantitative",scale: {domain: [42,43]}, axis: {title: "latitude"}}
}
})
Insert cell
Insert cell
// Load a file locally, search tweets with the word "happy"
viewof hashtag_text = html`<input type=file * ">`
Insert cell
tweetsStringsHash= Files.text(hashtag_text)
Insert cell
tweetsHasgDF = Object.values(JSON.parse(tweetsStringsHash))
Insert cell
Insert cell
locationHashCount={
let count= z.filter(r => r.group!= "", z.gbCount("group", z.groupBy(d => d.location, tweetsHasgDF)))
return z.sortByCol('count', 'des', count)
}
Insert cell
cleanHashTweets = {
let newLocations = []
let locations = z.getCol("location", tweetsHasgDF)
for (let id in locations) {
if (locations[id].toLowerCase().includes('united states')){
newLocations.push('United States')
} else if (locations[id].toLowerCase().includes('usa')) {
newLocations.push('United States')
} else{
newLocations.push(locations[id])
}
}
return z.addCol("cleanLocations", newLocations, tweetsHasgDF)
}
Insert cell
z.unique(z.getCol("hashtag",cleanHashTweets ))
Insert cell
Insert cell
Insert cell
cleaned =
{
let notNull=z.filter(r => r.lat != null, cleanHashTweets)
let hashCount=[]
for (var i=0; i<notNull.length; i++){
var Count=notNull[i]["hashtag"].length
hashCount.push(Count)
}
return z.addCol("hashtagCount", hashCount, notNull)
}
Insert cell
vegalite({
data: {values: cleaned},
mark: "point",
encoding: {
x: {field: "lon", type: "quantitative"},
y: {field: "lat", type: "quantitative"},
color:{field: "hashtagCount", type: "nominal",title:"Number of hashtags"}
}
})
Insert cell
Insert cell
Insert cell
Insert cell
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