orientation="y" When Aes Y Is Discrete¶import pandas as pd
from lets_plot import *
from lets_plot.mapping import as_discrete
LetsPlot.setup_html()
df = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/gapminder.csv')
albania_df = df.loc[df['country'] == 'Albania']
df.head(3)
orientation="y" Work¶gggrid([
ggplot(df, aes('continent', 'pop', fill='continent')) + geom_bar() + ggtitle('Default'),
ggplot(df, aes('pop', 'continent', fill='continent')) + geom_bar() + ggtitle('Automatic setting orientation="y"')
], ncol=2)
as_discrete()¶If you're using continuous data for the Y-axis, the plot may be incorrect. Try marking the Y-axis data with the as_discrete() function, this will rotate your geom.
gggrid([
ggplot(albania_df) + geom_bar(aes('lifeExp', 'year'), stat='sum', size=0),
ggplot(albania_df) + geom_bar(aes('lifeExp', as_discrete('year')), stat='sum', size=0)
], ncol=2)
Such manipulations with automatic rotation can be done for geoms geom_boxplot(), geom_violin(), geom_bar() and geom_lollipop().
gggrid([
ggplot(df, aes('continent', 'lifeExp', fill='continent')) + geom_boxplot(),
ggplot(df, aes('lifeExp', 'continent', fill='continent')) + geom_boxplot(),
ggplot(df, aes('continent', 'lifeExp', fill='continent')) + geom_violin(),
ggplot(df, aes('lifeExp', 'continent', fill='continent')) + geom_violin(),
ggplot(albania_df, aes(as_discrete('year'), 'lifeExp')) + geom_lollipop(stat='sum', size=2),
ggplot(albania_df, aes('lifeExp', as_discrete('year'))) + geom_lollipop(stat='sum', size=2),
], ncol=2)
stat='boxplot', stat='boxplot_outlier', stat_summary()¶gggrid([
ggplot(df, aes('continent', 'lifeExp')) + geom_pointrange(stat='boxplot'),
ggplot(df, aes('lifeExp', 'continent')) + geom_pointrange(stat='boxplot'),
ggplot(df, aes('continent', 'lifeExp')) + geom_pointrange(stat='boxplot_outlier'),
ggplot(df, aes('lifeExp', 'continent')) + geom_pointrange(stat='boxplot_outlier'),
ggplot(df, aes('continent', 'lifeExp')) + stat_summary(),
ggplot(df, aes('lifeExp', 'continent')) + stat_summary()
], ncol=2)