Gadfly: multiple y axes in one plot

1,358 views
Skip to first unread message

Sven Mesecke

unread,
Feb 28, 2014, 10:20:17 AM2/28/14
to julia...@googlegroups.com
I'd like to replicate the behavior of matlab's `plotyy` in Gadfly, i.e., I'm trying to plot data with very different `y` axes but the same `x` axes on the same plot, any idea of how to get this done? `layer` always seems to use the same base `y` axis.

Thanks for any pointers,

Sven

Harlan Harris

unread,
Feb 28, 2014, 11:07:19 AM2/28/14
to julia...@googlegroups.com
Don't do it. It's not good data visualization practice, and is explicitly and intentionally not supported in most grammar of graphics implementations. See, for one recent post I have handy: http://junkcharts.typepad.com/junk_charts/2014/02/a-message-worth-repeating.html

Two good alternatives are to stack/facet the graphs, one on top of each other, or to normalize the Y values so that they're proportions of their initial values. The latter's pretty common in financial plots, where Y=100 for X=min(X) for all series, so they start at the same point and then diverge.


David

unread,
Feb 28, 2014, 11:53:33 AM2/28/14
to julia...@googlegroups.com
For Harlan's first alternative, what you want is: http://dcjones.github.io/Gadfly.jl/geom_subplot_grid.html

Here's an example (below). We should put an example like this (with free_y_axis=true) in the documentation.

For some reason the call with (free_y_axis=true) is giving me trouble in an IJulia/IPython notebook, but work for me everywhere else. (I'm pasting the error message below.) 

-David



using DataFrames
using Gadfly

widedf = DataFrame(x = [1:10], var1 = [1:10], var2 = [1:10].^2)

longdf = stack(widedf, [:var1, :var2])

# this isn't what we want b/c the scales are the same:
plot(longdf, ygroup="variable", x="x", y="value", Geom.subplot_grid(Geom.point))

# this is what we want, but for some reason it isn't working for me in iPython notebook
plot(longdf, ygroup="variable", x="x", y="value", Geom.subplot_grid(Geom.point, free_y_axis=true))




# argument range must not be empty
#  in maximum_rgn at reduce.jl:336
#  in text_extents at /Users/david/.julia/v0.3/Compose/src/fontfallback.jl:82
#  in render at /Users/david/.julia/v0.3/Gadfly/src/guide.jl:587
#  in render_prepared at /Users/david/.julia/v0.3/Gadfly/src/Gadfly.jl:712
#  in render at /Users/david/.julia/v0.3/Gadfly/src/geom/subplot.jl:201
#  in render_prepared at /Users/david/.julia/v0.3/Gadfly/src/Gadfly.jl:705
#  in render at /Users/david/.julia/v0.3/Gadfly/src/Gadfly.jl:656
#  in writemime at /Users/david/.julia/v0.3/Gadfly/src/Gadfly.jl:755
#  in sprint at io.jl:467
#  in display_dict at /Users/david/.julia/v0.3/IJulia/src/execute_request.jl:35
--
David J. Chudzicki
blog.davidchudzicki.com
dch...@gmail.com
(518) 366-7303

Data Scientist
Kaggle (we're hiring!)

Keith Campbell

unread,
Feb 28, 2014, 3:32:25 PM2/28/14
to julia...@googlegroups.com
For telemetry and other engineering test data, it can be important to compare many time series in their measured units.  In that context, views like those described at:


are of real use.

Sven Mesecke

unread,
Mar 4, 2014, 7:54:47 AM3/4/14
to julia...@googlegroups.com
Thanks to everyone for the feedback, I actually tend to agree that the use of two different y axes in the same plot should be discouraged. Sometimes it's just too convenient...
In the end, I used `Geom.subplot_grid` with a `Geom.bar` plot which worked fine. However, it only worked with `position=:dodge` as `:stack` messed up y axis rescaling with the grouped bar plot (see the [github issue](https://github.com/dcjones/Gadfly.jl/issues/220)).

Thanks again,

Sven

Tim Holy

unread,
Mar 4, 2014, 9:56:32 AM3/4/14
to julia...@googlegroups.com
Somehow, I didn't find the reasoning in that particular example very
convincing. The real problem in that example---which the post completely
ignores---is that neither of the axes starts at 0. (The zeros should also
align between the two axes.) I can't see the objection to displaying the
actual units of each signal; the only difference between that and normalizing
to some value (say, the initial one) is that the latter throws information
away; you'd end up with the same lines in the end.

--Tim

Jason Merrill

unread,
Mar 4, 2014, 11:38:11 AM3/4/14
to julia...@googlegroups.com
I think the Bode diagram from Electrical Engineering is a pretty good use of double y axes. It's a plot of circuit response showing both amplitude (in db) and phase (in deg) vs frequency.

You can certainly do it as a stack, but over-plotting the curves allows even easier comparisons of important frequencies for the response, and saves some space.

Note that in this case, you would typically not have the axes share a 0.

Reply all
Reply to author
Forward
0 new messages