Files | Size | Format | Created | Updated | License | Source |
---|---|---|---|---|---|---|
2 | 125kB | csv zip | 5 years ago | 5 years ago | vega-datasets |
Download files in this dataset
File | Description | Size | Last changed | Download |
---|---|---|---|---|
barley | 4kB | csv (4kB) , json (9kB) | ||
vega-views-tutorial-grouping_zip | Compressed versions of dataset. Includes normalized CSV and JSON data with original data and datapackage.json. | 8kB | zip (8kB) |
This is a preview version. There might be more data in the original version.
Field Name | Order | Type (Format) | Description |
---|---|---|---|
yield | 1 | number | |
variety | 2 | string | |
year | 3 | number | |
site | 4 | string |
Use our data-cli tool designed for data wranglers:
data get https://datahub.io/examples/vega-views-tutorial-grouping
data info examples/vega-views-tutorial-grouping
tree examples/vega-views-tutorial-grouping
# Get a list of dataset's resources
curl -L -s https://datahub.io/examples/vega-views-tutorial-grouping/datapackage.json | grep path
# Get resources
curl -L https://datahub.io/examples/vega-views-tutorial-grouping/r/0.csv
curl -L https://datahub.io/examples/vega-views-tutorial-grouping/r/1.zip
If you are using R here's how to get the data you want quickly loaded:
install.packages("jsonlite", repos="https://cran.rstudio.com/")
library("jsonlite")
json_file <- 'https://datahub.io/examples/vega-views-tutorial-grouping/datapackage.json'
json_data <- fromJSON(paste(readLines(json_file), collapse=""))
# get list of all resources:
print(json_data$resources$name)
# print all tabular data(if exists any)
for(i in 1:length(json_data$resources$datahub$type)){
if(json_data$resources$datahub$type[i]=='derived/csv'){
path_to_file = json_data$resources$path[i]
data <- read.csv(url(path_to_file))
print(data)
}
}
Note: You might need to run the script with root permissions if you are running on Linux machine
Install the Frictionless Data data package library and the pandas itself:
pip install datapackage
pip install pandas
Now you can use the datapackage in the Pandas:
import datapackage
import pandas as pd
data_url = 'https://datahub.io/examples/vega-views-tutorial-grouping/datapackage.json'
# to load Data Package into storage
package = datapackage.Package(data_url)
# to load only tabular data
resources = package.resources
for resource in resources:
if resource.tabular:
data = pd.read_csv(resource.descriptor['path'])
print (data)
For Python, first install the `datapackage` library (all the datasets on DataHub are Data Packages):
pip install datapackage
To get Data Package into your Python environment, run following code:
from datapackage import Package
package = Package('https://datahub.io/examples/vega-views-tutorial-grouping/datapackage.json')
# print list of all resources:
print(package.resource_names)
# print processed tabular data (if exists any)
for resource in package.resources:
if resource.descriptor['datahub']['type'] == 'derived/csv':
print(resource.read())
If you are using JavaScript, please, follow instructions below:
Install data.js
module using npm
:
$ npm install data.js
Once the package is installed, use the following code snippet:
const {Dataset} = require('data.js')
const path = 'https://datahub.io/examples/vega-views-tutorial-grouping/datapackage.json'
// We're using self-invoking function here as we want to use async-await syntax:
;(async () => {
const dataset = await Dataset.load(path)
// get list of all resources:
for (const id in dataset.resources) {
console.log(dataset.resources[id]._descriptor.name)
}
// get all tabular data(if exists any)
for (const id in dataset.resources) {
if (dataset.resources[id]._descriptor.format === "csv") {
const file = dataset.resources[id]
// Get a raw stream
const stream = await file.stream()
// entire file as a buffer (be careful with large files!)
const buffer = await file.buffer
// print data
stream.pipe(process.stdout)
}
}
})()
This is an example dataset that demonstrates how to build visualizations using the “Vega Graph Spec”. We are using Yields of Barley Based on the Trellis Display - one of the examples from vega editor - and displaying it here, on DataHub with small modifications in vega-spec.
We assume that you are familiar with what datapackage.json is and its specifications.
To create graphs for your tabular data, the datapackage.json
should include the views
attribute that is used for visualizations.
If you are familiar with Vega specifications, you probably would like to use all its features. To use it, inside views
you should set specType
to "vega"
and define some graph specifications in spec
property. See below for more details.
This is the views
property that is used for this page:
{
...,
"views": [
{
"title": "DEMO Vega spec view",
"resources": ["barley"],
"specType": "vega",
"spec": {
"$schema": "https://vega.github.io/schema/vega/v3.0.json",
"width": 200,
"padding": 5,
"signals": [
{
"name": "offset",
"value": 15
},
{
"name": "cellHeight",
"value": 100
},
{
"name": "height",
"update": "6 * (offset + cellHeight)"
}
],
"data": [
{
"name": "barley"
}
],
"scales": [
{
"name": "gscale",
"type": "band",
"range": [
0,
{
"signal": "height"
}
],
"round": true,
"domain": {
"data": "barley",
"field": "site",
"sort": {
"field": "yield",
"op": "median",
"order": "descending"
}
}
},
{
"name": "xscale",
"type": "linear",
"nice": true,
"range": "width",
"round": true,
"domain": {
"data": "barley",
"field": "yield"
}
},
{
"name": "cscale",
"type": "ordinal",
"range": "category",
"domain": {
"data": "barley",
"field": "year"
}
}
],
"axes": [
{
"orient": "bottom",
"scale": "xscale",
"zindex": 1
}
],
"legends": [
{
"stroke": "cscale",
"title": "Year",
"padding": 4,
"encode": {
"symbols": {
"enter": {
"strokeWidth": {
"value": 2
},
"size": {
"value": 50
}
}
}
}
}
],
"marks": [
{
"name": "site",
"type": "group",
"from": {
"facet": {
"data": "barley",
"name": "sites",
"groupby": "site"
}
},
"encode": {
"enter": {
"y": {
"scale": "gscale",
"field": "site",
"offset": {
"signal": "offset"
}
},
"height": {
"signal": "cellHeight"
},
"width": {
"signal": "width"
},
"stroke": {
"value": "#ccc"
}
}
},
"scales": [
{
"name": "yscale",
"type": "point",
"range": [
0,
{
"signal": "cellHeight"
}
],
"padding": 1,
"round": true,
"domain": {
"data": "barley",
"field": "variety",
"sort": {
"field": "yield",
"op": "median",
"order": "descending"
}
}
}
],
"axes": [
{
"orient": "left",
"scale": "yscale",
"tickSize": 0,
"domain": false,
"grid": true,
"encode": {
"grid": {
"enter": {
"strokeDash": {
"value": [
3,
3
]
}
}
}
}
},
{
"orient": "right",
"scale": "yscale",
"tickSize": 0,
"domain": false
}
],
"marks": [
{
"type": "symbol",
"from": {
"data": "sites"
},
"encode": {
"enter": {
"x": {
"scale": "xscale",
"field": "yield"
},
"y": {
"scale": "yscale",
"field": "variety"
},
"stroke": {
"scale": "cscale",
"field": "year"
},
"strokeWidth": {
"value": 2
},
"size": {
"value": 50
}
}
}
}
]
},
{
"type": "text",
"from": {
"data": "site"
},
"encode": {
"enter": {
"x": {
"field": "width",
"mult": 0.5
},
"y": {
"field": "y"
},
"fontSize": {
"value": 11
},
"fontWeight": {
"value": "bold"
},
"text": {
"field": "datum.site"
},
"align": {
"value": "center"
},
"baseline": {
"value": "bottom"
},
"fill": {
"value": "#000"
}
}
}
}
]
}
}
]
}
You can use almost the same specifications inside spec
attribute, that are used for setting the Vega graphs. Only difference is that in data
property, url
and path
attributes are moved out. Instead we use name
attribute to reference to a resource. Note, how we created a new object inside data
property - {"name": "flights-airport"}
to reference it to a resource (this names are identical to names of resources):
...
"spec": {
...
"data": [
{
"name": "barley"
}
...
]
...
}
Outside of spec
attribute there are some other important parameters to note:
Attribute | Type | Description |
---|---|---|
name | String | Unique identifier for view within list of views. |
title | String | Title for the graph. |
resources | Array | Data sources for this spec. It can be either resource name or index. By default it is the first resource. |
specType | String | Available options: simple, vega, plotly (Required). |