secondary y-axis

6,431 views
Skip to first unread message

deeps

unread,
Jan 7, 2010, 1:25:41 PM1/7/10
to ggplot2
Is it possible to add a secondary y-axis using ggplot. i don't have
the problem of gird lines because i am using a blank theme.

any help is greatly appreciated.

thanks,
Deeps.

hadley wickham

unread,
Jan 7, 2010, 4:02:07 PM1/7/10
to deeps, ggplot2
> Is it possible to add a secondary y-axis using ggplot. i don't have
> the problem of gird lines because i am using a blank theme.

Not currently no. And it will never be possible to add a secondary
scale, just an secondary axes that is a transformation of the primary.

Hadley


--
http://had.co.nz/

Andreas Christoffersen

unread,
Jan 8, 2010, 5:17:38 AM1/8/10
to hadley wickham, ggplot2
Would that include e.g. scaled units? - e.g. like the line chart here:
http://junkcharts.typepad.com/junk_charts/2009/12/tiger-tiger.html

baptiste auguie

unread,
Jan 8, 2010, 5:42:59 AM1/8/10
to ggplot2
On Thu, Jan 7, 2010 at 10:02 PM, hadley wickham <h.wi...@gmail.com> wrote:
>> Is it possible to add a secondary y-axis using ggplot. i don't have
>> the problem of gird lines because i am using a blank theme.
>
> Not currently no.  And it will never be possible to add a secondary
> scale, just an secondary axes that is a transformation of the primary.


Would this restriction still allow making a graph like this?
http://blog.revolution-computing.com/2010/01/r-package-growth.html

The second y-axis is a transformation of the first (identity) but the
breaks are placed at different locations (it's often the case with
secondary axes).

Best,

baptiste

Hadley Wickham

unread,
Jan 8, 2010, 7:52:00 AM1/8/10
to Andreas Christoffersen, ggplot2
Yes. That is a transformation of the original scale.
Hadley

On Friday, January 8, 2010, Andreas Christoffersen

--
http://had.co.nz/

Hadley Wickham

unread,
Jan 8, 2010, 7:53:45 AM1/8/10
to baptiste auguie, ggplot2

Yes. You'd be able to modify all properties of the scale. It must be
trained on the same data.

Hadley

--
http://had.co.nz/

baptiste auguie

unread,
Jan 11, 2010, 5:11:00 AM1/11/10
to Hadley Wickham, ggplot2
What will you do with the grid lines? I can see three options,

1- allow selection of the axis (axes) for which we have grid lines,
with default the left and bottom axis breaks

2- keep the grid lines tied to the left (bottom) axis ; the right axis
(top) would only have small ticks and labels

3- have grid lines for both axes (resulting in a visual mess...)

Of course one could always add a first layer with annotate("hline",
xmin= -Inf, xmax=Inf, ...) if he's not happy with the lines.

Best,

baptiste

Hadley Wickham

unread,
Jan 11, 2010, 9:29:03 AM1/11/10
to baptiste auguie, ggplot2
Hi Baptiste,

I think the way that'd it work is that the grid lines would be set by
the primary axis, which could positioned anywhere. If you wanted
something different, you could turn off gridlines with the theming
system and then draw your own with geom_vline/geom_hline.

Hadley

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2
>

--
http://had.co.nz/

Samuel Dennis

unread,
May 8, 2011, 10:24:01 PM5/8/11
to ggp...@googlegroups.com, baptiste auguie
Hadley

Sorry to resurrect such an old thread, but I would like to ask whether it is necessary to restrict the proposed secondary y axis to only displaying a transformation of the first. I fully agree that in most situations secondary axes are confusing and best avoided. However everything has its place.

In my case I have time-series data to display in a main plot, and would like to display climatic data below it in a supportive role. I could have three graphs (data, rainfall and temperature), but this would be very cramped. It would be simpler to have one main data plot, with a subplot for rainfall (blue bars) and temperature (red line) on two axes - this is a common and very clear way of plotting climatic data.

The reader will never be comparing rainfall with temperature, rather is comparing rainfall or temperature trends with the data, so there will be little potential for misinterpretation. There is far greater potential for misinterpretation if the y axes must be shortened to accommodate a third graph, as this makes it more difficult for the reader to discern differences in the data.

I have created these plots in base graphics in the past but would like to be able to move to ggplot as it requires considerably less coding.

Every rule has an exception. Deliberate restrictions in usage to enforce what in most circumstances is correct are very frustrating for those confronted with the exception.

Thanks for an excellent package in all other respects.

Samuel

Hadley Wickham

unread,
May 16, 2011, 9:58:24 AM5/16/11
to ggp...@googlegroups.com, baptiste auguie
> Every rule has an exception. Deliberate restrictions in usage to enforce
> what in most circumstances is correct are very frustrating for those
> confronted with the exception.

I understand your point, and your frustration, but it's extremely
unlikely that this sort of dual-scale plot will ever be possible in
ggplot2. I have so little development time to spend on ggplot2 that I
can only spend on those problems that I think are most important, and
while I might be persuaded to not hate double-axis plots, you'll never
be able to persuade me enough to love them enough to implement them.

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Ben Bolker

unread,
May 14, 2011, 9:14:34 AM5/14/11
to ggp...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 11-05-16 09:58 AM, Hadley Wickham wrote:
>> Every rule has an exception. Deliberate restrictions in usage to enforce
>> what in most circumstances is correct are very frustrating for those
>> confronted with the exception.
>
> I understand your point, and your frustration, but it's extremely
> unlikely that this sort of dual-scale plot will ever be possible in
> ggplot2. I have so little development time to spend on ggplot2 that I
> can only spend on those problems that I think are most important, and
> while I might be persuaded to not hate double-axis plots, you'll never
> be able to persuade me enough to love them enough to implement them.
>
> Hadley
>

To chime in here: I think the point that may not be appreciated is
that the restriction that secondary y-axes are only allowed as
transformations of a primary axis is not (I think) a *deliberate*
restriction, but one that stems from the (sophisticated/complicated)
architecture of ggplot2.

I would also be tempted to nominate Hadley's reply as a fortune()
candidate: in the generic format,

I understand your point, and your frustration, but it's extremely

unlikely that [FEATURE X] will ever be possible in [PACKAGE Y]. I have
so little development time to spend on [PACKAGE Y] that I can only spend


on those problems that I think are most important, and while I might be

persuaded to not hate [FEATURE X], you'll never be able to persuade me
enough to love them enough to implement [them/it].

(substitute your favorite combination of feature and package here ...)

Also note that we are talking here about a hypothetical extension
(arbitrary secondary y-axes) to a hypothetical extension (y-axes based
on transformations of a primary y-axis) that doesn't exist yet (and may
itself be pretty far down on Hadley's to-do list) ... if I were you and
wanted arbitrary secondary y-axes, I would start hacking around with the
grid.xaxis and grid.yaxis functions in the grid package and see if I
could adjust the layout of an existing ggplot (converted a grid object
via ggplotGrob() -- see the align.plots function in the ggExtra package
for examples of plot-hacking)

Ben Bolker
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk3OgDoACgkQc5UpGjwzenNOZACZAXWSjX0rUJOCZexAcZy5vNQb
gScAnjtbTD2boBgeouWTCnkYdN3ZcZPQ
=DsRW
-----END PGP SIGNATURE-----

baptiste auguie

unread,
May 16, 2011, 5:10:02 PM5/16/11
to Ben Bolker, ggp...@googlegroups.com


A major issue here is that ggplot2 does not use Grid axes but defines
its own (as I recall, not working with native units); it makes such
custom changes more difficult, if at all possible. My version of the
above request would be to facilitate the work of people willing to
extend ggplot2 with grid functions (as lattice does); I believe it is
already going this way in the current package overhaul.

Best,

baptiste

>  Ben Bolker
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk3OgDoACgkQc5UpGjwzenNOZACZAXWSjX0rUJOCZexAcZy5vNQb
> gScAnjtbTD2boBgeouWTCnkYdN3ZcZPQ
> =DsRW
> -----END PGP SIGNATURE-----
>

> --
> You received this message because you are subscribed to the ggplot2 mailing list.

> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

Adam_L...@keybank.com

unread,
May 16, 2011, 5:22:04 PM5/16/11
to baptiste auguie, ggp...@googlegroups.com

I think the logical first step is to persuade Hadley that he doesn't hate secondary axes. ;)



Adam Loveland




baptiste auguie <bapt...@googlemail.com>
Sent by: ggp...@googlegroups.com

05/16/2011 05:10 PM

To
Ben Bolker <bbo...@gmail.com>
cc
ggp...@googlegroups.com
Subject
Re: secondary y-axis


This communication may contain privileged and/or confidential information. It is intended solely for the use of the addressee. If you are not the intended recipient, you are strictly prohibited from disclosing, copying, distributing or using any of this information. If you received this communication in error, please contact the sender immediately and destroy the material in its entirety, whether electronic or hard copy. This communication may contain nonpublic personal information about consumers subject to the restrictions of the Gramm-Leach-Bliley Act. You may not directly or indirectly reuse or redisclose such information for any purpose other than to provide the services for which you are receiving the information. 127 Public Square, Cleveland, OH 44114


If you prefer not to receive future e-mail offers for products or services from Key
send an e-mail to mailto:DNERe...@key.com with 'No Promotional E-mails' in the
SUBJECT line.

Samuel Dennis

unread,
May 16, 2011, 5:31:56 PM5/16/11
to Ben Bolker, ggp...@googlegroups.com
I don't expect someone else to develop a new feature for me. I was just under the impression from the earlier posts that Hadley was already intending to implement a secondary axis but only as a transformation of the first axis.

"And it will never be possible to add a secondary scale, just an secondary axes that is a transformation of the primary." (Hadley)

This may no longer be on the priority list though, which I understand.

My point is that as soon as a secondary axis is available, even if trained on the same data, the first thing some users will start doing is producing scaled datasets where one variable matches the secondary axis, as a dirty workaround, and you'll immediately have dual-axis plots available in ggplot2. Any feature will be pushed to its limits, that's what people do.

Knowing how users will mistreat it anyway, it might be useful to code the second axis more generically from the start to allow people to extend it in this way more flexibly. Or follow baptiste's suggestion to provide a even more generic method of modifying this.

But if the transformed secondary axis has dropped off the plans entirely my suggestion may be irrelevant. Or maybe it's just unpalatable (good point Adam)!

Thanks,
Samuel

--
You received this message because you are subscribed to the ggplot2 mailing list.

Shannon McFadyen

unread,
Mar 16, 2014, 7:16:11 PM3/16/14
to ggp...@googlegroups.com
Has there been any update with plotting a secondary y-axis using ggplot? 

Dennis Murphy

unread,
Mar 17, 2014, 2:48:30 AM3/17/14
to Shannon McFadyen, ggplot2
Earlier this month, Hadley and Winston announced that ggplot2 was
going to be feature frozen since they've decided to move future
development to the ggvis package (not yet on CRAN, but on GitHub). The
answer to your question, then, is that there will not be any support
for secondary axes in ggplot2, now or in the future.

Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages