Galary & API

VegaLite.jl 文档绘图例子: http://fredo-dedup.github.io/VegaLite.jl/stable/index.html

VegaLite 官方 Example Galary: https://vega.github.io/vega-lite/examples/

VegaLite API 文档( JSON 格式): https://vega.github.io/vega-lite/docs/

mark 特性

// Json 版本
{
  ...
  "mark": {
    "type": ...,       // mark
    ...
  },
  ...
}
# Julia 版本
@vlplot(
    mark={
        typ=:..., # 注意不能使用 Julia 预留关键字 type
        ...
    }
)

狭义的 mark 指的是 mark 键下的 type 字段

类型mark -> typexycolorshapesize
线图:line
轨迹图:trace
垂线、水平线图:rule
空心散点图:point
圆形实心散点图:circle
方形实心散点图:square
文字标注图:texttext=:var
柱状图:bar
直方图:barx={:a, bin=true}y=”count()”
热力图、填充图:rectx=”x:o”y=”y:o”color=:z
area plot (面积堆积图):area
strip plot(分布散点图):tickx=:xy=”y:o”
地理图:geoshape

广义的 mark 包括:typestyleclip 三部分。

详细 mark 特性参看: https://vega.github.io/vega-lite/docs/mark.html

几个栗子

运行例子代码前需要加载以下库 ↓

using VegaLite, VegaDatasets

Example1

dataset("stocks") |>
@vlplot(
    :trail, # 等价于 mark = :trail 等价于 mark={typ=:trail}
    x={
        "date:t",
        axis={format="%Y"}
    },
    y=:price,
    size=:price,
    color=:symbol
)

这里写图片描述

Example2

dataset("unemployment-across-industries") |>
@vlplot(
    :area, # 等价于 mark = :area 等价于 mark={typ=:area}
    width=300, height=200,
    x={
        "yearmonth(date)",
        axis={
            domain=false,
            format="%Y",
            tickSize=0
        }
    },
    y={
        "sum(count)",
        axis=nothing,
        stack=:center
    },
    color={
        :series,
        scale={scheme="category20b"}
    }
)

这里写图片描述

Example3

cars |>
    @vlplot(
        y="Origin:o",
        x="Cylinders:o",
        config={
            scale={bandPaddingInner=0, bandPaddingOuter=0},
            text={baseline=:middle}
        }
    ) +
    @vlplot(
        :rect, # 等价于 mark = :rect 等价于 mark={typ=:rect}
        color="count()") +
    @vlplot(
        :text, # 等价于 mark = :text 等价于 mark={typ=:text}
        text="count()",
        color={
            condition={
                test="datum['count_*'] > 100",
                value=:black
            },
            value=:white
        }
)

这里写图片描述

Example4

dataset("population") |>
@vlplot(
    transform=[{
        aggregate=[
            {op=:q1, field=:people, as=:lowerBox},
            {op=:q3, field=:people, as=:upperBox},
            {op=:median, field=:people, as=:midBox},
            {op=:min, field=:people, as=:lowerWhisker},
            {op=:max, field=:people, as=:upperWhisker}
        ],
        groupby=[:age]
    }]
) +
@vlplot(
    mark={:rule, style=:boxWhisker},
    y={"lowerWhisker:q", axis={title="population"}},
    y2="lowerBox:q",
    x="age:o"
) +
@vlplot(
    mark={:rule, style=:boxWhisker},
    y="upperBox:q",
    y2="upperWhisker:q",
    x="age:o"
) +
@vlplot(
    mark={:bar, style=:box},
    y="lowerBox:q",
    y2="upperBox:q",
    x="age:o",
    size={value=5}
) +
@vlplot(
    mark={:tick, style=:boxMid},
    y="midBox:q",
    x="age:o",
    color={value=:white},
    size={value=5}
)

这里写图片描述

Example5

us10m = dataset("us-10m").path
unemployment = dataset("unemployment.tsv").path

p = @vlplot(
    :geoshape, # mark
    width=500, height=300,
    data={
        url=us10m,
        format={
            typ=:topojson,
            feature=:counties
        }
    },
    transform=[{
        lookup=:id,
        from={
            data=unemployment,
            key=:id,
            fields=["rate"]
        }
    }],
    projection={
        typ=:albersUsa
    },
    color="rate:q"
) 

这里写图片描述

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐