Flipping Coordinates#
import pandas as pd
from lets_plot import *
LetsPlot.setup_html()
df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
print(df.shape)
df.head()
(234, 12)
| 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 |
| 4 | 5 | audi | a4 | 2.8 | 1999 | 6 | auto(l5) | f | 16 | 26 | p | compact |
coord_flip()#
p = ggplot(df) + theme(axis_title_x='blank')
box_plot = p + \
geom_boxplot(aes(as_discrete("class", order=1, order_by="..middle.."), "hwy"),
color="#579673", fill="#9ac0b3", size=1.5) + \
ggsize(700, 300)
box_plot + ggtitle("Default")
box_plot + ggtitle("Flipped") + coord_flip()
bar_plot = p + \
geom_bar(aes(as_discrete("manufacturer", order_by="..count.."), fill="class", color="class"),
size=1.5, alpha=0.7, width=0.7) + \
ggsize(800, 300)
bar_plot + ggtitle("Default")
bar_plot + ggtitle("Flipped") + coord_flip()
Parameter flip#
point_plot = ggplot(df) + \
geom_point(aes("displ", "hwy", fill="class"),
alpha=.5, shape=21,
position=position_jitter(width=.2, height=.2, seed=42))
point_plot + coord_fixed(ratio=.1) + ggtitle("coord_fixed()")
point_plot + coord_fixed(ratio=10, flip=True) + ggtitle("coord_fixed(flip=True)")