
:orphan:
:html_theme.sidebar_secondary.remove:

.. This document is auto-generated by the altair-gallery extension. Do not modify directly.

.. _gallery_groupby-map:


Grouped Points with Proportional Symbols Map
============================================
This is a layered geographic visualization that groups points by state.


.. altair-plot::
    :remove-code:
    


    import altair as alt
    from altair.datasets import data

    airports = data.airports.url
    states = alt.topo_feature(data.us_10m.url, feature='states')

    # US states background
    background = alt.Chart(states).mark_geoshape(
        fill='lightgray',
        stroke='white'
    ).properties(
        width=500,
        height=300
    ).project('albersUsa')

    # Airports grouped by state
    points = alt.Chart(airports).transform_aggregate(
        latitude='mean(latitude)',
        longitude='mean(longitude)',
        count='count()',
        groupby=['state']
    ).mark_circle().encode(
        longitude='longitude:Q',
        latitude='latitude:Q',
        size=alt.Size('count:Q', title='Number of Airports'),
        color=alt.value('steelblue'),
        tooltip=['state:N','count:Q']
    ).properties(
        title='Number of airports in US'
    )

    background + points


.. tab-set::

    .. tab-item:: Method syntax
        :sync: method

        .. code:: python


            import altair as alt
            from altair.datasets import data

            airports = data.airports.url
            states = alt.topo_feature(data.us_10m.url, feature='states')

            # US states background
            background = alt.Chart(states).mark_geoshape(
                fill='lightgray',
                stroke='white'
            ).properties(
                width=500,
                height=300
            ).project('albersUsa')

            # Airports grouped by state
            points = alt.Chart(airports, title='Number of airports in US').transform_aggregate( 
                latitude='mean(latitude)',
                longitude='mean(longitude)',
                count='count()',
                groupby=['state']
            ).mark_circle().encode(
                longitude='longitude:Q',
                latitude='latitude:Q',
                size=alt.Size('count:Q').title('Number of Airports'),
                color=alt.value('steelblue'),
                tooltip=['state:N','count:Q']
            )

            background + points


    .. tab-item:: Attribute syntax
        :sync: attribute

        .. code:: python


            import altair as alt
            from altair.datasets import data

            airports = data.airports.url
            states = alt.topo_feature(data.us_10m.url, feature='states')

            # US states background
            background = alt.Chart(states).mark_geoshape(
                fill='lightgray',
                stroke='white'
            ).properties(
                width=500,
                height=300
            ).project('albersUsa')

            # Airports grouped by state
            points = alt.Chart(airports).transform_aggregate(
                latitude='mean(latitude)',
                longitude='mean(longitude)',
                count='count()',
                groupby=['state']
            ).mark_circle().encode(
                longitude='longitude:Q',
                latitude='latitude:Q',
                size=alt.Size('count:Q', title='Number of Airports'),
                color=alt.value('steelblue'),
                tooltip=['state:N','count:Q']
            ).properties(
                title='Number of airports in US'
            )

            background + points
