Use the labels
parameter to create an annotated Barchart.
import pandas as pd
from lets_plot import *
LetsPlot.setup_html()
mpg_df = pd.read_csv ("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
mpg_df.head(4)
Unnamed: 0 | manufacturer | model | displ | year | cyl | trans | drv | cty | hwy | fl | class | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | audi | a4 | 1.8 | 1999 | 4 | auto(l5) | f | 18 | 29 | p | compact |
1 | 2 | audi | a4 | 1.8 | 1999 | 4 | manual(m5) | f | 21 | 29 | p | compact |
2 | 3 | audi | a4 | 2.0 | 2008 | 4 | manual(m6) | f | 20 | 31 | p | compact |
3 | 4 | audi | a4 | 2.0 | 2008 | 4 | auto(av) | f | 21 | 30 | p | compact |
gggrid([
ggplot(mpg_df) + geom_bar(aes("trans"),
labels=layer_labels().line('@..count..')),
ggplot(mpg_df) + geom_bar(aes("trans", y="..n.."), stat="sum", size=0,
labels=layer_labels().line('@..proppct..')) + coord_flip()
], widths=[1, 1.2])
ggplot(mpg_df, aes('class', fill='drv')) + \
geom_bar(labels=layer_labels().line('@..proppct..').format('..proppct..', '{d} %')) + \
ggsize(700, 400)
ggplot(mpg_df, aes('class', fill='drv')) + \
geom_bar(labels=layer_labels().line('@..proppct..').format('..proppct..', '{d} %'),
position='fill') + \
coord_flip() + \
ggsize(700, 400)
Too thin for labels to fit in.
ggplot(mpg_df, aes('class', fill='drv')) + \
geom_bar(labels=layer_labels().line('@..proppct..').format('..proppct..', '{d} %'),
position='dodge') + \
ggsize(700, 400)