Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve encoding section #2735

Merged
merged 26 commits into from
Dec 19, 2022
Merged
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c1a2ae9
Remove key channel as not relevant for Altair and not documented anyway
binste Dec 1, 2022
f391466
Add explanation and description to detail channel
binste Dec 1, 2022
80fe108
Add explanation on difference between datum and value
binste Dec 1, 2022
93f09f5
Remove key and description also from channel option sections
binste Dec 1, 2022
a272039
Update doc/user_guide/encoding.rst
binste Dec 8, 2022
4ca2084
Update doc/user_guide/encoding.rst
binste Dec 8, 2022
92549a9
Update doc/user_guide/encoding.rst
binste Dec 8, 2022
b9b3258
Update doc/user_guide/encoding.rst
binste Dec 8, 2022
6dbe37a
Fix usage of alt.value in rule example
binste Dec 8, 2022
ceea0b0
Merge branch 'master' into improve_encoding_sections
binste Dec 15, 2022
01c5dd4
Move section on themes from configuration to customization page
binste Dec 15, 2022
e485e49
Collapse object tables by default
binste Dec 15, 2022
a42f7db
Move encodings page into subfolder
binste Dec 15, 2022
77677b2
Move content on channels and channel options into separate subpages
binste Dec 15, 2022
1be0fd7
Introduce both concepts of a channel and channel options in the begin…
binste Dec 15, 2022
2c89913
Format code
binste Dec 15, 2022
2d01fa1
Minor language improvements
binste Dec 15, 2022
9399659
Update doc/user_guide/encodings/index.rst
binste Dec 19, 2022
7a76bef
Update doc/user_guide/encodings/index.rst
binste Dec 19, 2022
495a0ee
Update doc/user_guide/encodings/index.rst
binste Dec 19, 2022
f69cb96
Update doc/user_guide/encodings/index.rst
binste Dec 19, 2022
2dcdc69
Update doc/user_guide/encodings/index.rst
binste Dec 19, 2022
1b53c09
Update doc/user_guide/encodings/index.rst
binste Dec 19, 2022
4932b0e
Update doc/user_guide/encodings/channels.rst
binste Dec 19, 2022
c68eb97
Add reference to faceted charts section
binste Dec 19, 2022
20568e0
Add introductory sentences before tables
binste Dec 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 113 additions & 8 deletions doc/user_guide/encoding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Encoding Channels
~~~~~~~~~~~~~~~~~

Altair provides a number of encoding channels that can be useful in different
circumstances; the following table summarizes them:
circumstances. The following tables summarize them:

Position Channels
^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -91,7 +91,6 @@ Text and Tooltip Channels
Channel Altair Class Description Example
======= ================ ======================== =========================================
text :class:`Text` Text to use for the mark :ref:`gallery_scatter_with_labels`
key :class:`Key` -- N/A
tooltip :class:`Tooltip` The tooltip value :ref:`gallery_scatter_tooltips`
======= ================ ======================== =========================================

Expand All @@ -107,12 +106,37 @@ href :class:`Href` Hyperlink for points :ref:`gallery_scatter_href`
Level of Detail Channel
^^^^^^^^^^^^^^^^^^^^^^^
binste marked this conversation as resolved.
Show resolved Hide resolved

Grouping data is an important operation in data visualization. For line and area marks,
mapping an unaggregated data field to any
non-position channel will group the lines and stacked areas by that field.
For aggregated plots, all unaggregated fields encoded are used as grouping fields
in the aggregation (similar to fields in ``GROUP BY`` in SQL).

The ``detail`` channel specifies an additional grouping field (or fields) for grouping
data without mapping the field(s) to any visual properties.

======= ================ =============================== =========================================
Channel Altair Class Description Example
======= ================ =============================== =========================================
detail :class:`Detail` Additional property to group by :ref:`gallery_ranged_dot_plot`
======= ================ =============================== =========================================

For example here is a line chart showing stock prices of 5 tech companies over time.
We map the ``symbol`` variable to ``detail`` to use them to group lines.

.. altair-plot::

import altair as alt
from vega_datasets import data

source = data.stocks()
alt.Chart(source).mark_line().encode(
x="date:T",
y="price:Q",
detail="symbol:N"
)


Order Channel
^^^^^^^^^^^^^

Expand Down Expand Up @@ -325,17 +349,17 @@ The :class:`Text` encoding accepts the following options:

.. altair-object-table:: altair.FieldOrDatumDefWithConditionStringFieldDefText

Description, Href, Tooltip, Url
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Href, Tooltip, Url
^^^^^^^^^^^^^^^^^^

The :class:`Description`, :class:`Href`, :class:`Tooltip`, and :class:`Url` encodings accept the following options:
The :class:`Href`, :class:`Tooltip`, and :class:`Url` encodings accept the following options:

.. altair-object-table:: altair.StringFieldDefWithCondition

Detail and Key
^^^^^^^^^^^^^^
Detail
^^^^^^

The :class:`Detail` and :class:`Key` encodings accept the following options:
The :class:`Detail` encoding accepts the following options:

.. altair-object-table:: altair.FieldDefWithoutScale

Expand Down Expand Up @@ -677,3 +701,84 @@ While the above examples show sorting of axes by specifying ``sort`` in the

Here the y-axis is sorted reverse-alphabetically, while the color legend is
sorted in the specified order, beginning with ``'Morris'``.

Datum and Value
~~~~~~~~~~~~~~~

So far we always mapped an encoding channel to a column in our dataset. However, sometimes
it is also useful to map to a single constant value. In Altair, you can do this with

* ``datum``, which describes a constant data value encoded via a scale
* ``value``, which describes an encoded constant visual value

binste marked this conversation as resolved.
Show resolved Hide resolved
``datum`` is particularly useful for annotating a specific data value.
For example, you can use it with a rule mark to highlight a
threshold value (e.g., 200 dollars stock price).
binste marked this conversation as resolved.
Show resolved Hide resolved

.. altair-plot::

import altair as alt
from vega_datasets import data

source = data.stocks()
base = alt.Chart(source)
lines = base.mark_line().encode(
x="date:T",
y="price:Q",
color="symbol:N"
)
rule = base.mark_rule(strokeDash=[2, 2]).encode(
y=alt.datum(300)
)

lines + rule
binste marked this conversation as resolved.
Show resolved Hide resolved

You can also use ``datum`` with :class:`DateTime`, for example, to highlight a certain year.
In addition, we will set the color for the rule to the same one as the line for the symbol ``MSFT``
with ``alt.datum("MSFT")``.

.. altair-plot::

import altair as alt
from vega_datasets import data

source = data.stocks()
base = alt.Chart(source)
lines = base.mark_line().encode(
x="date:T",
y="price:Q",
color="symbol:N"
)
rule = base.mark_rule(strokeDash=[2, 2]).encode(
x=alt.datum(alt.DateTime(year=2006)),
color=alt.datum("MSFT")
)

lines + rule


Similar to when mapping to a data column, when using ``datum`` different encoding channels
may support ``band``, ``scale``, ``axis``, ``legend``, ``format``, or ``condition`` properties.
However, data transforms (e.g. ``aggregate``, ``bin``, ``timeUnit``, ``sort``) cannot be applied.

Expanding on the example above, if you would want to color the ``rule`` mark regardless of
the color scale used for the lines, you can use ``value``, e.g. ``alt.value("red")``:

.. altair-plot::

import altair as alt
from vega_datasets import data

source = data.stocks()
base = alt.Chart(source)
lines = base.mark_line().encode(
x="date:T",
y="price:Q",
color="symbol:N"
)
rule = base.mark_rule(strokeDash=[2, 2]).encode(
x=alt.datum(alt.DateTime(year=2006)),
color=alt.value("red")
)

lines + rule