The layer orientation

Some geoms treat each axis differently and, thus, can have two orientations.

The orientation parameter specifies the axis that the layer' stat and geom should run along (x-axis by default).

In [1]:
import numpy as np
import pandas as pd

from lets_plot import *
from lets_plot.mapping import as_discrete
LetsPlot.setup_html()

geom_bar()

In [2]:
mpg = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")

base = ggplot(mpg) + ggsize(800, 300)
manufacturer_mapping = as_discrete('manufacturer', order_by='..count..')
hide_x_label = theme(axis_title_x='blank')
hide_y_label = theme(axis_title_y='blank')
In [3]:
# orientation "x" (default) : "manufacturer" is mapped to the x-axis.
orientation_x = base + geom_bar(aes(x=manufacturer_mapping, fill='class'), color='white')
orientation_x + hide_x_label
Out[3]:
In [4]:
orientation_x + coord_flip() + hide_x_label
Out[4]:
In [5]:
# Orientation "y".
# The "manufacturer" is mapped to the y-axis.

base_y = base + hide_y_label
base_y + geom_bar(aes(y=manufacturer_mapping, fill='class'), color='white', orientation="y")
Out[5]:

geom_boxplot()

In [6]:
base_y + geom_boxplot(aes(y='manufacturer', x='cty'), width=0.7, orientation="y")
Out[6]:

geom_violin()

In [7]:
ggplot(mpg) + geom_violin(aes(y="drv", x='cty', fill="drv"), trim=False, orientation="y") \
            + geom_boxplot(aes(y="drv", x='cty'), fill="white", alpha=0.5, width=0.3, orientation="y")
Out[7]:

Continuous variables

In [8]:
#
# Density estimates.
#

np.random.seed(0)

cov0=[[1, -.8], 
     [-.8, 1]]     
cov1=[[ 10, .1],
       [.1, .1]]

x0, y0 = np.random.multivariate_normal(mean=[-2,0], cov=cov0, size=200).T
x1, y1 = np.random.multivariate_normal(mean=[0,1], cov=cov1, size=200).T

data = dict(
    x = np.concatenate((x0,x1)),
    y = np.concatenate((y0,y1)),
    c = ["A"]*200 + ["B"]*200
)
In [9]:
p = ggplot(data, aes("x", "y", color="c")) + geom_point()
p
Out[9]:

geom_density()

In [10]:
p + geom_density(size=2)
Out[10]:
In [11]:
p + geom_density(size=2, orientation="y")
Out[11]:

geom_histogram()

In [12]:
p + geom_histogram(fill="rgba(0,0,0,0)")
Out[12]:
In [13]:
p + geom_histogram(alpha=0, orientation="y", show_legend=False)
Out[13]:
In [14]:
p + geom_freqpoly(size=2)
Out[14]:
In [15]:
p + geom_freqpoly(size=2, orientation="y")
Out[15]:

geom_smooth()

In [16]:
p + geom_smooth()
Out[16]:
In [17]:
p + geom_smooth() + coord_flip()
Out[17]:
In [18]:
p + geom_smooth(orientation="y")
Out[18]:

geom_boxplot()

In [19]:
p + geom_boxplot(x=-10, color="black") \
  + geom_boxplot(y=-3, color="black", orientation="y")
Out[19]:

geom_violin()

In [20]:
p + geom_violin(x=-10, color="black", alpha=0) \
  + geom_violin(y=-3, color="black", alpha=0, orientation="y")
Out[20]:
In [21]:
p + geom_violin(aes(fill="c"), y=-2, color="black", 
                alpha=0.3, position="identity", width=3, show_legend=False,
                orientation="y")
Out[21]: