ggplot2 2.2.0 - Secondary Y axis

2,747 views
Skip to first unread message

Kousik Kundu

unread,
Oct 2, 2016, 9:00:10 AM10/2/16
to ggplot2
Hi,
   I am glad to see that ggplot2 2.2.0 introduces the secondary axis on the plot using sec.axis argument:
ggplot(mpg, aes(displ, hwy)) + 
  geom_point() + 
  scale_y_continuous(
    "mpg (US)", 
    sec.axis = sec_axis(~ . * 1.20, name = "mpg (UK)")
  )
At the moment, it only allows a formula in the sec.axis. I was wondering is it possible to use a separate column to use as a secondary axis -

For example, how would I able to plot when -

x axis = mpg$displ
y1 axis = mpg$hwy
y2 axis = mpg$cty

Any help would be highly appreciated. Thank you very much.


Best regards,
Kousik

Roman Luštrik

unread,
Oct 2, 2016, 11:48:20 AM10/2/16
to Kousik Kundu, ggplot2
Blog post on the new version is explicitly saying:

You can display a secondary axis that is a one-to-one transformation of the primary axis with the sec.axis argument:

​Basically it's saving user from him or herself as this appears to be the only legitimate use of sec. axis. Secondary axes are frowned upon and should probably not be used (or used sparingly). AFAIK, this feature request has been turned down previously. If you need to present two variables, use two plots/facets.

Cheers,
Roman​
 

--
--
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+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
In God we trust, all others bring data.

Sam Albers

unread,
Oct 11, 2016, 12:27:37 PM10/11/16
to Roman Luštrik, Kousik Kundu, ggplot2
Hi all,

This is not a reply to argue about ggplot2's attitude regarding
secondary y-axes but rather a request for a reference that outlines
the reasons against it as a practice? I am often asked to create
figures with two y-axes and sometimes struggle to come up with a
cohesive argument against the practice. I've come to trust the
opinions on this list but wonder if there is a penultimate reference
or source that talks about the pitfalls on the 2 y-axis approach.

By the way - some REALLY nice changes to the new version of ggplot2

Regards,

Sam
>> 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.
>
>
>
>
> --
> In God we trust, all others bring data.
>
> --
> --
> 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.

Ben Bolker

unread,
Oct 11, 2016, 2:37:11 PM10/11/16
to ggp...@googlegroups.com

There are a few reference links (from Junk Charts and from Stephen
Few) in this StackOverflow answer:
http://stackoverflow.com/questions/6142944/how-can-i-plot-with-2-different-y-axes
Reply all
Reply to author
Forward
0 new messages