1

I am trying to reverse the secondary X axis on top of my ggplot.

ggplot( data=MasterTable, aes(x=Concentration, y=Signal, color=factor(Assay))) + scale_x_continuous("Chemical 1", sec.axis = sec_axis(~ . *1, name = "Chemical 2"), scale_x_reverse(limits=c(400,0))) 

If you remove the last section of the code (scale_x_reverse...) it makes a plot with a secondary that is identical to the bottom X axis. I have managed to reverse the bottom axis but this also reverses the top axis. I am looking to only reverse the top axis.

Any help on this would be really appreciated.

Thanks!

enter image description here

1
  • 1
    Please provide a reproducible example(incl. data). Commented Oct 20, 2017 at 20:08

1 Answer 1

5

Here is a possibile solution:

MasterTable <- data.frame(Concentration=rep(c(0,50,100,200,300, 350, 400),2), Signal=c(11800,13000,12000,12000,16000,15500,15570,11600,11700,8000,8000,6000,4000,3000), Assay=rep(1:2,each=7)) library(ggplot2) # Reverse Signal vector of the blue series (for Assay =1) MasterTable$Signal[MasterTable$Assay==1] <- rev(MasterTable$Signal[MasterTable$Assay==1]) ggplot(data=MasterTable, aes(x=Concentration, y=Signal, color=factor(Assay))) + geom_line(lwd=1) + geom_point(size=3) + guides(color='none') + scale_x_continuous('Chemical 1 (nM)', trans='reverse', sec.axis = sec_axis(~ 400 - . , name='Chemical 2 (nM)')) 

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

This works very well! Thank you! To summarize, reverse the bottom axis and subtract the top axis by the highest value of your bottom axis.
@Marty999 Yes! The substraction is required for avoiding to reverse the second axis.
This works for the OP's problem, just be careful if you're using anything that's not a simple reversal.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.