import pandas as pd
from lets_plot import *
from lets_plot.geo_data import *
LetsPlot.setup_html()
mpg = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
mpg.head()
states_gdf = geocode_states().scope("UK").get_boundaries(6)
states_gdf.head()
Typical use case: changing limits of axes.
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()
Typical use case: stretching a plot along one of the axes.
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()
.
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()
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.
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.
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()
Typical use case: geospatial data.
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()
.