Skip to content

Instantly share code, notes, and snippets.

@casallas
Last active August 29, 2015 14:07
Show Gist options
  • Save casallas/9620aef26b30cb7addae to your computer and use it in GitHub Desktop.
Save casallas/9620aef26b30cb7addae to your computer and use it in GitHub Desktop.
ezpolar in ggplot2

Mimicing Matlab's ezpolar in R, with ggplot2

In MATLAB/octave plotting stuff in polar coordinates is really easy, e.g. the cardioid $r(\theta) = 1+cos(\theta)$, $0 < \theta < 2\pi$ is as simple as:

ezpolar('1+cos(t)',[0,2*pi])

In ggplot, more verbose is needed

qplot(c(0, 2*pi), geom="blank") + # Specify range in a blank geom
  stat_function(fun=function(t) 1 + cos(t), geom="line") + # Specify the function
  coord_polar(direction=-1, start=3*pi/2) + # Counterclockwise (-1), and specify what 6 o'clock should be in radians
  scale_x_continuous(breaks=seq(0, 2*pi, by=pi/6), labels=seq(0, 360, by=30)) # Place breaks every pi/6 (30 deg), use labels in degrees

and, to be honest, MATLAB's output still looks prettier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment