Example #1
0
def ggplot(data=None, mapping=None):
    """
    Create a new ggplot plot

    Parameters
    ----------
    data : dictionary or pandas DataFrame, optional
        Default dataset to use for the plot. If not specified, must be supplied in each layer added to the plot.
    mapping : dictionary, optional
        Default list of aesthetic mappings to use for the plot. If not specified, must be supplied in each layer
        added to the plot.
     Returns
    -------
        plot specification
    Notes
    -----
        ggplot() initializes a ggplot object. It can be used to declare the input data frame for a graphic and to
        specify the set of plot aesthetics intended to be common throughout all subsequent layers unless specifically
        overridden.
        ggplot() is typically used to construct a plot incrementally, using the + operator to add layers to the
        existing ggplot object. This is advantageous in that the code is explicit about which layers are added
        and the order in which they are added. For complex graphics with multiple layers, initialization with
        ggplot() is recommended.
        There are three common ways to invoke ggplot (see examples below):
        - ggplot(dat,aes(x,y)) :
            This method is recommended if all layers use the same data and the same set of aesthetics, although
            this method can also be used to add a layer using data from another data frame.
        - ggplot(dat) :
             This method specifies the default data frame to use for the plot, but no aesthetics are defined up front.
             This is useful when one data frame is used predominantly as layers are added, but the aesthetics may vary
             from one layer to another.
        - ggplot() :
            This method initializes a skeleton ggplot object which is fleshed out as layers are added. This method is
            useful when multiple data frames are used to produce different layers, as is often the case in complex
            graphics.
        ggplot() with no layers defined will produce an error message: "No layers in plot"
     Examples
    ---------
    >>> import numpy as np
    >>> import pandas as pd
    >>> from lets_plot import *
    >>> x = np.random.uniform(-1, 1, size=100)
    >>> y = np.random.normal(size=100)
    >>> dat = pd.DataFrame({'x': x, 'y': 25 * x ** 2 + y})
    >>> dat['class'] = ['0' if dat['x'][i] < 0 else '1' for i in range(100)]
    >>> # three ways to invoke ggplot, producing the same output:
    >>> # (1)
    >>> ggplot(dat, aes(x='x', y='y')) + layer()
    >>> # (2)
    >>> ggplot(dat) + layer()
    >>> # (3)
    >>> ggplot() + layer('point', 'identity', dat)

    """
    if isinstance(data, FeatureSpec):
        raise ValueError(
            "Object {!r} is not acceptable as 'data' argument in ggplot()".
            format(data.kind))

    data, mapping, data_meta = as_annotated_data(data, mapping)

    return PlotSpec(data, mapping, scales=[], layers=[], **data_meta)
Example #2
0
def ggplot(data=None, mapping=None):
    """
    Create a new ggplot plot.

    Parameters
    ----------
    data : dict or `DataFrame`
        Default dataset to use for the plot. If not specified,
        must be supplied in each layer added to the plot.
    mapping : `FeatureSpec`
        Default list of aesthetic mappings to use for the plot.
        If not specified, must be supplied in each layer added to the plot.

    Returns
    -------
    `PlotSpec`
        Plot specification.

    Note
    ----
    `ggplot()` initializes a ggplot object.
    It can be used to declare the input data frame for a graphic and
    to specify the set of plot aesthetics intended to be common
    throughout all subsequent layers unless specifically overridden.

    `ggplot()` is typically used to construct a plot incrementally,
    using the + operator to add layers to the existing ggplot object.
    This is advantageous in that the code is explicit about which layers
    are added and the order in which they are added. For complex graphics
    with multiple layers, initialization with `ggplot()` is recommended.

    There are three common ways to invoke ggplot (see examples below):

    - `ggplot(data, aes(x, y))`: This method is recommended if all layers use the same data and the same set of aesthetics, although this method can also be used to add a layer using data from another data frame.
    - `ggplot(data)`: This method specifies the default data frame to use for the plot, but no aesthetics are defined up front. This is useful when one data frame is used predominantly as layers are added, but the aesthetics may vary from one layer to another.
    - `ggplot()`: This method initializes a skeleton ggplot object which is fleshed out as layers are added. This method is useful when multiple data frames are used to produce different layers, as is often the case in complex graphics.

    `ggplot()` with no layers defined will produce an error message:
    "No layers in plot".

    Examples
    --------
    .. jupyter-execute::
        :linenos:
        :emphasize-lines: 11, 13, 15

        import numpy as np
        from lets_plot import *
        LetsPlot.setup_html()
        np.random.seed(42)
        n = 100
        x = np.random.uniform(-1, 1, size=n)
        y = np.random.normal(size=n)
        data = {'x': x, 'y': 25 * x ** 2 + y}
        # three ways to invoke ggplot, producing the same output:
        # (1)
        ggplot(data, aes(x='x', y='y')) + geom_point()
        # (2)
        ggplot(data) + geom_point(aes(x='x', y='y'))
        # (3)
        ggplot() + geom_point(aes(x='x', y='y'), data=data)

    """
    if isinstance(data, FeatureSpec):
        raise ValueError(
            "Object {!r} is not acceptable as 'data' argument in ggplot()".
            format(data.kind))

    if is_geocoder(data):
        data = data.get_geocodes()

    data, mapping, data_meta = as_annotated_data(data, mapping)

    return PlotSpec(data, mapping, scales=[], layers=[], **data_meta)
Example #3
0
def get_data_meta(data):
    _, _, data_meta = as_annotated_data(data, None)
    return data_meta['data_meta']