ggplot2:反转辅助连续x轴 [英] ggplot2: Reversing secondary continuous x axis

查看:285
本文介绍了ggplot2:反转辅助连续x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反转ggplot顶部的辅助X轴.

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)))

如果删除代码的最后一部分(scale_x_reverse ...),则将绘制一个具有与底部X轴相同的辅助图形的图形.我设法反转了底轴,但是这也反转了顶轴.我只想反转上轴.

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.

谢谢!

推荐答案

这是一个可能的解决方案:

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)')) 

这篇关于ggplot2:反转辅助连续x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆