# Coordinate Systems¶

## Preparation¶

In [1]:
import pandas as pd

from lets_plot import *
from lets_plot.geo_data import *
LetsPlot.setup_html()

The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).

In [2]:
mpg = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")

Out[2]:
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
In [3]:
states_gdf = geocode_states().scope("UK").get_boundaries(6)

Out[3]:
state found name geometry
0 Cymru / Wales Cymru / Wales MULTIPOLYGON (((-3.25195 51.38550, -3.27393 51...
1 Scotland Scotland MULTIPOLYGON (((-7.44873 56.95696, -7.44873 56...
2 England England MULTIPOLYGON (((-5.05371 50.14875, -5.07568 50...
3 Northern Ireland Northern Ireland MULTIPOLYGON (((-6.30615 54.09806, -6.32812 54...

## 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()


## Fixed Coordinates¶

Typical use case: stretching a plot along one of the axes.

In [5]:
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 [6]:
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 [7]:
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 [8]:
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 [9]:
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 [10]:
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().