#!/usr/bin/env python # coding: utf-8 # # Customize Legend Appearance # In[1]: from IPython.display import Image import pandas as pd from lets_plot import * # In[2]: LetsPlot.setup_html() # In[3]: display(Image("images/theme_legend_scheme.png")) # In[4]: df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv") print(df.shape) df.head() # ## Legend Position # # Parameters to customize the legend position and justification: # # - `legend_position` - the position of legends; # # - `legend_justification` - anchor point for positioning legend; # # - `legend_direction` - layout of items in legends. # In[5]: p1 = ggplot(df, aes("displ", "hwy")) + geom_point(aes(color="cty", shape="drv"), size=5) p1 # In[6]: # Draw legend below the plot area p1 + theme(legend_position='bottom') # In[7]: # Specify position, justification and orientation p1 + theme(legend_position=[1, 1], legend_justification=[1, 1], legend_direction='horizontal') # ## Legend Spacing and Margins # # Parameters to customize the legend spacing and margins: # # - `legend_box` - arrangement of multiple legends ("horizontal" or "vertical"); # # - `legend_box_just` - justification of each legend within the overall bounding box ("top", "bottom", "left", "right", "center"); # # - `legend_box_spacing` - spacing between plotting area and legend box; # # - `legend_margin` - margin around each legend; # # - `legend_spacing` - spacing between legends, `legend_spacing_x`/`legend_spacing_y` - in the horizontal/vertical direction. # In[8]: p2 = ggplot(df, aes('displ', 'cty')) + \ geom_point(aes(fill='drv', size='hwy'), shape=21) + \ scale_size(range=[1, 10], breaks=[15, 30, 40]) + \ theme(legend_position='bottom', legend_background=element_rect(size=1)) p2 # In[9]: # legend_margin p2 + theme(legend_margin=[10, 20]) # In[10]: # legend_box p2 + theme(legend_box='horizontal') # In[11]: # legend_box_just p2 + theme(legend_box_just='center') # In[12]: # legend_box_spacing p2 + theme(legend_box='horizontal', legend_box_spacing=50) # In[13]: # legend_spacing p2 + theme(legend_box='horizontal', legend_spacing_x=50) # ## Legend Key Parameters # # Parameters to customize the legend key: # # - `legend_key` - background underneath legend keys, set with `element_rect()`; # # - `legend_key_size` - size of legend keys; # # - `legend_key_width` - key background width; # # - `legend_key_height` - key background height; # # - `legend_key_spacing` - spacing between legend keys; # # - `legend_key_spacing_x` - spacing in the horizontal direction; # # - `legend_key_spacing_y` - spacing in the vertical direction. # In[14]: p3 = ggplot(df, aes(x='hwy')) + \ geom_dotplot(aes(fill='class'), color='pen') + \ theme(legend_background=element_rect(size=0.5)) p3 # Add background underneath legend keys: # In[15]: p4 = p3 + theme(legend_key=element_rect(fill='#efedf5', color='#756bb1')) p4 # Change size of legend keys: # In[16]: p4 + theme(legend_key_size=30) # In[17]: p5 = p4 + theme(legend_key_width=80, legend_key_height=20) p5 # Add spacing between legend keys: # In[18]: p5 + theme(legend_key_spacing_y=10)