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")
p = ggplot(mpg, aes('cty', 'hwy', color='drv')) + geom_point() + labs(color='Drive type')
p
hjust_left = element_text(hjust=0.0)
hjust_middle = element_text(hjust=0.5)
hjust_right = element_text(hjust=1.0)
h = 420
# Horizontal justification for plot title, subtitle, caption, legend and axis titles
def with_hjust(hjust):
return theme(title=hjust, axis_title=hjust, plot_caption=hjust, legend_title=hjust)
p2 = p + labs(caption='The plot caption')
bunch = GGBunch()
bunch.add_plot(p2 + with_hjust(hjust_left) + ggtitle("All titles with hjust = 0.0"), 0, 0)
bunch.add_plot(p2 + with_hjust(hjust_middle) + ggtitle("All titles with hjust = 0.5"), 0, h)
bunch.add_plot(p2 + with_hjust(hjust_right) + ggtitle("All titles with hjust = 1.0"), 0, 2*h)
bunch.show()
# Vertical justification for legend title
vjust_bottom = element_text(vjust=0.0)
vjust_center = element_text(vjust=0.5)
vjust_top = element_text(vjust=1.0)
p3 = p + theme(legend_position='bottom') + scale_color_discrete(guide=guide_legend(ncol=1))
def with_vjust(vjust):
return theme(legend_title=vjust)
bunch = GGBunch()
bunch.add_plot(p3 + with_vjust(vjust_bottom) + ggtitle("Legend title with vjust = 0.0"), 0, 0)
bunch.add_plot(p3 + with_vjust(vjust_center) + ggtitle("Legend title with vjust = 0.5"), 0, h)
bunch.add_plot(p3 + with_vjust(vjust_top) + ggtitle("Legend title with vjust = 1.0"), 0, 2*h)
bunch.show()