#!/usr/bin/env python # coding: utf-8 # # Coordinate Systems # ## Preparation # In[1]: import pandas as pd from lets_plot import * from lets_plot.geo_data import * LetsPlot.setup_html() # In[2]: mpg = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv") mpg.head() # In[3]: states_gdf = geocode_states().scope("UK").get_boundaries(6) states_gdf.head() # ## Cartesian Coordinates # # Typical use case: changing limits of axes. # In[4]: p = ggplot(mpg, aes("cty", "hwy")) + geom_point() p1 = p + ggtitle("Default") p2 = p + coord_cartesian(xlim=[10, 30], ylim=[10, 40]) + ggtitle("With coord_cartesian()") bunch = GGBunch() bunch.add_plot(p1, 0, 0, 500, 375) bunch.add_plot(p2, 500, 0, 500, 375) bunch.show() # ## Polar Coordinates # # Typical use case: for pie charts and polar plots. # In[5]: p = ggplot(mpg, aes("cty", "hwy")) + geom_point() p1 = p + ggtitle("Default") p2 = p + coord_polar() + ggtitle("With coord_polar()") bunch = GGBunch() bunch.add_plot(p1, 0, 0, 500, 375) bunch.add_plot(p2, 500, 0, 500, 375) bunch.show() # ## Fixed Coordinates # # Typical use case: stretching a plot along one of the axes. # In[6]: p = ggplot(mpg, aes("cty", "hwy")) + geom_point() p1 = p + ggtitle("Default") p2 = p + coord_fixed(ratio=1) + ggtitle("With coord_fixed()") bunch = GGBunch() bunch.add_plot(p1, 0, 0, 500, 375) bunch.add_plot(p2, 500, 0, 300, 375) bunch.show() # Fixing coordinates could be combined with changing limits as for `coord_cartesian()`. # In[7]: p = ggplot(mpg, aes("cty", "hwy")) + geom_point() p1 = p + coord_fixed(ratio=1) + ggtitle("Stretching only") p2 = p + coord_fixed(ratio=1, xlim=[10, 30], ylim=[10, 40]) + ggtitle("With changing limits") bunch = GGBunch() bunch.add_plot(p1, 0, 0, 300, 375) bunch.add_plot(p2, 300, 0, 300, 375) bunch.show() # ## Flipping Coordinates # In[8]: p = ggplot(mpg, aes(x="fl")) + geom_bar() p1 = p + ggtitle("Default") p2 = p + coord_flip() + ggtitle("With coord_flip()") bunch = GGBunch() bunch.add_plot(p1, 0, 0, 500, 375) bunch.add_plot(p2, 500, 0, 300, 375) bunch.show() # Flipping could also be combined with changing limits. # In[9]: p = ggplot(mpg, aes(x="fl")) + geom_bar() p1 = p + coord_flip() + ggtitle("Flipping only") p2 = p + coord_flip(ylim=[0, 300]) + ggtitle("With changing limits") bunch = GGBunch() bunch.add_plot(p1, 0, 0, 300, 375) bunch.add_plot(p2, 300, 0, 300, 375) bunch.show() # Also you can add flipping as an option to other coordinate systems. # In[10]: p = ggplot(mpg, aes(x="fl")) + geom_bar() p1 = p + coord_flip() + ggtitle("Flipping through coord_flip()") p2 = p + coord_cartesian(flip=True) + ggtitle("Flipping through 'flip=True'") bunch = GGBunch() bunch.add_plot(p1, 0, 0, 300, 375) bunch.add_plot(p2, 300, 0, 300, 375) bunch.show() # ## Map Coordinates # # Typical use case: geospatial data. # In[11]: p = ggplot() + geom_polygon(aes(fill="state"), data=states_gdf) p1 = p + ggtitle("Default") p2 = p + coord_map() + ggtitle("With coord_map()") bunch = GGBunch() bunch.add_plot(p1, 0, 0, 500, 375) bunch.add_plot(p2, 500, 0, 500, 375) bunch.show() # Parameters `xlim`, `ylim`, `flip` are also available for `coord_map()`.