import pandas as pd
from lets_plot import *
LetsPlot.setup_html()
mpg = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
mpg
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 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
229 | 230 | volkswagen | passat | 2.0 | 2008 | 4 | auto(s6) | f | 19 | 28 | p | midsize |
230 | 231 | volkswagen | passat | 2.0 | 2008 | 4 | manual(m6) | f | 21 | 29 | p | midsize |
231 | 232 | volkswagen | passat | 2.8 | 1999 | 6 | auto(l5) | f | 16 | 26 | p | midsize |
232 | 233 | volkswagen | passat | 2.8 | 1999 | 6 | manual(m5) | f | 18 | 26 | p | midsize |
233 | 234 | volkswagen | passat | 3.6 | 2008 | 6 | auto(s6) | f | 17 | 26 | p | midsize |
234 rows × 12 columns
p = (
ggplot(mpg, aes('displ', 'hwy')) +
geom_point() +
labs(
title = "Fuel efficiency\nfor most popular models of car",
subtitle = "Period 1999-2008",
caption = "MPG Dataset",
x="Engine displacement\n(litres)",
y = "Highway\nmiles per gallon"
) +
theme_classic() +
theme(plot_background=element_rect(size=1))
)
p
# Change margins around plot title, subtitle, caption and axis titles
p + theme(plot_title=element_text(margin=[15, None, None]),
plot_subtitle=element_text(margin=[None, None, 10]),
plot_caption=element_text(margin=[None, None, 15]),
axis_title_x=element_text(margin=[10, None, None]),
axis_title_y=element_text(margin=[None, 10, None, 15]))
# Change margins around axis tick labels
p + theme(axis_text_x=element_text(margin=[10, None, 15]),
axis_text_y=element_text(margin=[None, 10, None, 15]))
# axis labels at different levels
p2 = ggplot(mpg, aes('class', 'hwy')) + geom_boxplot()
p2
p2 + theme(axis_text_x=element_text(margin=[10, None]))
# with rotation
p2 + ggsize(550, 400)
p2 + ggsize(550, 400)+ theme(axis_text_x=element_text(margin=[10, None, None]))