#!/usr/bin/env python # coding: utf-8 # In[1]: import pandas as pd from lets_plot import * LetsPlot.setup_html() # In[2]: mpg = pd.read_csv ("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv") mpg # In[3]: p = ggplot(mpg) + geom_point(aes('displ', 'hwy', color = 'drv')) p # In[4]: p + ggtitle("The plot title using 'ggtitle()'") # In[5]: p + labs(title = "The plot title using 'labs()'") # In[6]: # Add subtitle using ggtitle() p + ggtitle("The plot title", subtitle = "The plot subtitle") # In[7]: # Add subtitle using labs() p + labs(title = "The plot title", subtitle = "The plot subtitle") # In[8]: # Add caption p2 = p + labs(title = "The plot title", subtitle = "The plot subtitle", caption = "The plot caption") p2 # In[9]: # Add color for title # 'title' applies to plot's title, subtitle, caption p2 + theme(title=element_text(color='blue')) # In[10]: # 'plot_title' will also apply to the subtitle p2 + theme(plot_title=element_text(color='blue')) # In[11]: # Set own colors p2 + theme( plot_title=element_text(color='blue'), plot_subtitle=element_text(color='red'), plot_caption=element_text(color='dark_green')) # In[12]: # 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')) # In[13]: # Legend title p1 = ggplot(mpg) + geom_point(aes('displ', 'hwy', color = 'cty', shape='drv'), size=4) p1 + labs(color='City mileage', shape='Drive type') # In[14]: # Change legend position p1 + labs(color='City mileage', shape='Drive type') + theme(legend_position='bottom') # In[15]: # Use multiple lines in legend titles p1 + labs(color='City mileage\n(mpg)', shape='Drive type\n(front/4/rear wheel)') # In[16]: p1 + theme(legend_position='bottom') \ + labs(color='City mileage\n(mpg)', shape='Drive type\n(front/4/rear wheel)') # In[17]: 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))