You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
scale_y_discrete() and scale_x_discrete() currently appear not to support the sec.axis argument of their scale_continuous() brethren.
sec.axis would be nice for discrete scales as well, such as when you have various ways to refer to factor levels (short and long) and the like.
Reacted by Peter Ellis, TJ Mahr, Sunantha Sethuraman, Thiago V. dos Santos, syan1, Jemma Stachelek, Isaac Shaffer, j-5chneider, Daniel Väisänen, Rudolf Cardinal and 68 more
I second this request, and for the same reasons as @maxheld83 mentioned. This would be a really nice feature, which in the current ggplot2 version cannot be achieved even with hacks.
Reacted by Rudolf Cardinal, Lukas, Tobias Opialla, A. Solomon Kurz, Mae Bruckbauer, Adam, Kyle Kumbier, James Goldie, chsuong, tkivisik and 1 more
The issue is still open, as far as I can tell. If somebody would like to contribute a PR, we'd certainly review it.
It didn't make sense to tackle this feature before @paleolimbot had completed his rewrite of position scales, but since that has happened and is merged I'd assume implementing discrete secondary axes shouldn't be too hard.
I think a guide extension is the way to go about this (actually, I think regular second axes should use this as well). It's not future proof since we're about to redo guides (post patch release), but you could hack this with the current system:
Reacted by Max Held, iagogv3, Rick Saporta, Thiago V. dos Santos, Paul Campbell, melissakey, werkstattcodes, Nick Negretti, Kristján Moore (Kris), Tobi Eh and 13 more
I also support this feature request ... even if the xaxis label could just be duplicated without any transformation ... this would be very useful for large / long plots to have the same label on the top and bottom (or left and right, respectively).
Reacted by Venkatesh Avula, Mark Heckmann, Simon Pearce, Diego F Coêlho, Hanne Oberman, Ludger Goeminne, David Ochoa, Rakesh Dodhia and Michael Piechotta
Agreed! This would be very useful for omics expression heatmaps, where you might want to refer to the accession number as well as the full title of a protein on your plot! +1
I think a guide extension is the way to go about this (actually, I think regular second axes should use this as well). It's not future proof since we're about to redo guides (post patch release), but you could hack this with the current system:
This works great, but I'm wondering how to modify the following line to use the values of a different variable:
guides(x.sec = guide_axis_label_trans(~paste("Ha!", .x)))
I want something like
guides(x.sec = guide_axis_label_trans(~Unit))
to call the values of my "Unit" character vector.
Any guidance would be much appreciated. and a +1 for secondary axes for discrete scales!
I find another way to build a second axis by package patchwork. you can build a new plot only contain your axis and patch togather.
Example for a new x axis in top:
library(patchwork)
p1=ggplot(data,aes(x,1)+scale_x_discrete(position = 'top')+
theme(plot.margin = unit(c(0,1,0,1),'cm'),axis.line.y = element_blank(),
axis.text.y = element_blank(),axis.ticks.y = element_blank())+
labs(y=NULL,x=NULL)
p1/p2 + plot_layout(heights = c(0,1))
p2 is your plot need to add axis
you also can change axes style as you want (like ggprism)
Activity
thiagoveloso commentedon May 10, 2019
I second this request, and for the same reasons as @maxheld83 mentioned. This would be a really nice feature, which in the current
ggplot2version cannot be achieved even with hacks.topialla commentedon Apr 15, 2020
Why was this closed? There still seems to be only this workaround available:
https://stackoverflow.com/a/45362497/1842673
Could this be implemented?
clauswilke commentedon Apr 15, 2020
The issue is still open, as far as I can tell. If somebody would like to contribute a PR, we'd certainly review it.
It didn't make sense to tackle this feature before @paleolimbot had completed his rewrite of position scales, but since that has happened and is merged I'd assume implementing discrete secondary axes shouldn't be too hard.
paleolimbot commentedon Apr 15, 2020
Famous last words!
I think a guide extension is the way to go about this (actually, I think regular second axes should use this as well). It's not future proof since we're about to redo guides (post patch release), but you could hack this with the current system:
Created on 2020-04-15 by the reprex package (v0.3.0)
rsaporta commentedon May 11, 2020
was searching for something similar and found the SO thread linking to here.
+1this useful feature request. Thanks @paleolimbot for the hackEnricGTorrents commentedon May 19, 2020
I also came here from SO looking for a way to duplicate the discrete y scale. +1 this would be a nice feature to see in future versions. Thanks!
matteodefelice commentedon Jul 20, 2020
I think it would be nice to have this feature.
saul-torres commentedon Nov 2, 2020
I have just arrived here from SO.
Please, it would be great to have this option.
marton-balazs-kovacs commentedon Jan 19, 2021
I would love this!
smped commentedon Mar 2, 2021
Yes, also a great idea from my perspective!
rocanja commentedon Jun 23, 2021
I also support this feature request ... even if the xaxis label could just be duplicated without any transformation ... this would be very useful for large / long plots to have the same label on the top and bottom (or left and right, respectively).
fgazzelloni commentedon Oct 25, 2021
I found this package:
{ggh4x},library(ggh4x)here is the resource
library(ggh4x)#> Loading required package: ggplot2df <- data.frame(x = seq(-3, 3, length.out = 6), y = LETTERS[1:6])g <- ggplot(df, aes(x, y)) +geom_col() +scale_x_continuous(breaks = -3:0, guide = "axis_truncated",sec.axis = dup_axis(breaks = 0:3, guide = "axis_truncated")) +theme(axis.line.x = element_line())g + guides(y.sec = guide_axis_manual(breaks = unit(c(1, 3), "cm"),labels = expression("treshold"^2, "annotation"[3]),label_colour = c("red", "blue"), label_size = c(8, 12)))Created on 2021-10-25 by the reprex package [(v2.0.1)If you run this code you'll see that you can change/customize your secondary axis even with discrete values.
Hope it helps.
1 remaining item
hanneoberman commentedon Mar 8, 2022
Agreed! Just here to leave my temporary solutions for others:
juliabandura commentedon Mar 18, 2022
Agreed! This would be very useful for omics expression heatmaps, where you might want to refer to the accession number as well as the full title of a protein on your plot! +1
chsuong commentedon Apr 25, 2022
Seconded.
kimlorenz commentedon Jun 29, 2022
Would also like to be able to duplicate categorical axes
mmpust commentedon Jul 29, 2022
I support this feature request. Thanks!
xiaofanliang commentedon Oct 10, 2022
Commenting to keep this in favor.
KateSchneider-FoodPol commentedon Dec 7, 2022
This works great, but I'm wondering how to modify the following line to use the values of a different variable:
guides(x.sec = guide_axis_label_trans(~paste("Ha!", .x)))
I want something like
guides(x.sec = guide_axis_label_trans(~Unit))
to call the values of my "Unit" character vector.
Any guidance would be much appreciated. and a +1 for secondary axes for discrete scales!
thanks!
kjfuller06 commentedon Jan 24, 2023
You should be able to run
guides(x.sec = guide_axis_label_trans(~paste0(levels(Unit))))Worked for my issue. Works great for second y axis too. Glad I found this! Thanks @paleolimbot !
teunbrand commentedon Jan 24, 2023
I also think it would be nice to move all secondary axis responsibility from the scales system to the guides system, once that has been overhauled.
enblacar commentedon Feb 13, 2023
Hi,
The
guide_axis_label_transhack worked wonders for me and fully synergises with theaxis.text.x.top(and similar) theme elements!Is there a plan to integrate this into a ggplot2 official release? Or is it already done and I have missed it?
Many thanks!
Enrique
Meech81 commentedon Mar 6, 2023
Has anyone seen a fix for this when using facets in ggplot?
Thanks
Michelle
abesolberg commentedon Mar 10, 2023
Is there a way to add a secondary axis title using the
guide_axis_label_transworkaround?Generalized commentedon Jul 11, 2023
Is there any good news about that? It's been 2019, now it's 2023...
liubiaodi commentedon Jan 1, 2024
I find another way to build a second axis by package patchwork. you can build a new plot only contain your axis and patch togather.
Example for a new x axis in top:
library(patchwork)
p1=ggplot(data,aes(x,1)+scale_x_discrete(position = 'top')+
theme(plot.margin = unit(c(0,1,0,1),'cm'),axis.line.y = element_blank(),
axis.text.y = element_blank(),axis.ticks.y = element_blank())+
labs(y=NULL,x=NULL)
p1/p2 + plot_layout(heights = c(0,1))
p2 is your plot need to add axis
you also can change axes style as you want (like ggprism)