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) + geom_point(aes('displ', 'hwy', color = 'drv'))
p
p + ggtitle("The plot title using 'ggtitle()'")
p + labs(title = "The plot title using 'labs()'")
# Add subtitle using ggtitle()
p + ggtitle("The plot title", subtitle = "The plot subtitle")
# Add subtitle using labs()
p + labs(title = "The plot title", subtitle = "The plot subtitle")
# Add caption
p2 = p + labs(title = "The plot title", subtitle = "The plot subtitle", caption = "The plot caption")
p2
# Add color for title
# 'title' applies to plot's title, subtitle, caption
p2 + theme(title=element_text(color='blue'))
# 'plot_title' will also apply to the subtitle
p2 + theme(plot_title=element_text(color='blue'))
# Set own colors
p2 + theme(
plot_title=element_text(color='blue'),
plot_subtitle=element_text(color='red'),
plot_caption=element_text(color='dark_green'))
# Multiple lines - using `\n`
p + labs(
title = "The plot title:\nFuel efficiency for most popular models of car",
subtitle = "The plot subtitle:\nPoints are colored by the type of drive train",
caption = "The plot caption:\nmpg dataset"
) + theme(plot_subtitle=element_text(color='gray'), plot_caption=element_text(color='light_gray'))
# Legend title
p1 = ggplot(mpg) + geom_point(aes('displ', 'hwy', color = 'cty', shape='drv'), size=4)
p1 + labs(color='City mileage', shape='Drive type')
# Change legend position
p1 + labs(color='City mileage', shape='Drive type') + theme(legend_position='bottom')
# Use multiple lines in legend titles
p1 + labs(color='City mileage\n(mpg)',
shape='Drive type\n(front/4/rear wheel)')
p1 + theme(legend_position='bottom') \
+ labs(color='City mileage\n(mpg)',
shape='Drive type\n(front/4/rear wheel)')
p1 + theme(legend_position='bottom') \
+ labs(color='City mileage\n(mpg)',
shape='Drive type\n(front/4/rear wheel)') \
+ scale_shape(guide=guide_legend(nrow=3))