In [1]:
%matplotlib inline
from ggplot import *
import pandas as pd
import numpy as np

Logarithmic Scales

ggplot allows you to adjust both the x and y axis to use a logarithmic scale. The scale_x_log or scale_y_log can be added to any plot. You can also adjust the type of logarithm that is used by providing a base parameter (i.e. scale_x_log(base=2) for natural log) to the function. If not specified log base 10 will be used.

In [2]:
diamonds.head()
Out[2]:
carat cut color clarity depth table price x y z
0 0.23 Ideal E SI2 61.5 55.0 326 3.95 3.98 2.43
1 0.21 Premium E SI1 59.8 61.0 326 3.89 3.84 2.31
2 0.23 Good E VS1 56.9 65.0 327 4.05 4.07 2.31
3 0.29 Premium I VS2 62.4 58.0 334 4.20 4.23 2.63
4 0.31 Good J SI2 63.3 58.0 335 4.34 4.35 2.75
In [3]:
ggplot(diamonds, aes(x='carat', y='price')) + geom_point()
Out[3]:
<ggplot: (285111113)>
In [4]:
ggplot(diamonds, aes(x='carat', y='price')) + \
    geom_point() + \
    scale_y_log()
Out[4]:
<ggplot: (273694445)>
In [ ]:
 
In [5]:
df = pd.DataFrame(dict(
        x=np.arange(1, 1000)
    ))
df['y'] = np.log(df.x)
df.head()
Out[5]:
x y
0 1 0.000000
1 2 0.693147
2 3 1.098612
3 4 1.386294
4 5 1.609438
In [6]:
ggplot(df, aes(x='x', y='y')) + geom_point()
Out[6]:
<ggplot: (285247637)>
In [7]:
ggplot(df, aes(x='x', y='y')) + geom_point() + scale_x_log()
Out[7]:
<ggplot: (285024853)>

Reversing Axes

If you find yourself in the position where you need to reverse an axis, you can do so using scale_x_reverse or scale_y_reverse.

In [8]:
ggplot(mtcars, aes(x='mpg')) + geom_histogram() + scale_x_reverse()
Out[8]:
<ggplot: (285218201)>
In [9]:
ggplot(mtcars, aes(x='mpg')) + geom_histogram() + scale_y_reverse()
Out[9]:
<ggplot: (290502653)>

Changing Coordinate Systems

You can switch between different coordinate systems using the coord_* family of layers. Just be careful that you're using the correct aesthetics! The available coordinate systems are:

  • coord_equal
  • coord_flip
  • coord_polar
  • coord_cartesian (this is the default, so you never explicitly invoke it)

coord_equal

coord_equal will make the x and y axes use the same scale. This is handy if you're comparing 2 variables together, or want a square-looking plot.

In [10]:
ggplot(aes(x='beef', y='pork'), data=meat) + \
    geom_point() + \
    coord_equal()
Out[10]:
<ggplot: (290784017)>
In [11]:
ggplot(aes(x='beef', y='pork'), data=meat) + \
    geom_point() + \
    geom_abline(slope=1, intercept=0, color='teal') + \
    coord_equal()
Out[11]:
<ggplot: (290926537)>

coord_flip

coord_flip will make the x axis the y axis and vice-versa. So taking the plot we just made and flipping it would look like this.

In [12]:
# sadly, this doesn't appear to work
ggplot(aes(x='beef', y='pork'), data=meat) + \
    geom_point() + \
    coord_flip()
Out[12]:
<ggplot: (290926389)>

coord_polar

coord_polar uses a polar coordinate system instead of cartesian.

In [13]:
df = pd.DataFrame({"x": np.arange(100)})
df['y'] = df.x * 10
df['z'] = ["a" if x%2==0 else "b" for x in df.x]

# polar coords
p = ggplot(df, aes(x='x', y='y')) + geom_point() + coord_polar()
print p
<ggplot: (291101857)>
In [14]:
ggplot(df, aes(x='x', y='y')) + geom_point() + geom_line() + coord_polar()
Out[14]:
<ggplot: (291107933)>
In [ ]: