似乎自ggplot2.2.2 update以来没有涵盖这个主题,其中旧的解决方案如this one和this one不再适用 . 幸运的是,这个过程比以前简单得多 . 一行代码,您有一个辅助Y轴(as shown here) .
但我不能在我的情节上获得辅助X轴......
我正在比较沿着沉积物核心的金属浓度的深度剖面 . 我希望将碳和磷酸盐浓度显示为金属浓度的背后 . 问题是碳和磷酸盐浓度都与金属的浓度不同 . 因此我需要第二个轴 .
主题如下(摘自this website):
theme_new <- theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"), strip.text.x = element_text(size=10, angle=0, vjust=0), strip.background = element_blank(), strip.text.y = element_text(angle = 0), legend.position="none",panel.border = element_blank(), axis.text.x=element_text(angle=45,hjust=1)) # Axis tick label angle
而且这个代码给了我第二个Y轴,即使我在X轴下指定它 .
ggplot(MasterTable)+
geom_line(aes(Depth,Conc.nM))+
geom_area(aes(Depth,Conc.uM, fill=Variable))+
scale_x_continuous("Depth (cm)", sec.axis = sec_axis(~ . *100, name = "Carbon & Phosphate"))+
scale_y_continuous("Metal concentration (nM)")+
coord_flip()+
theme_new+
theme(legend.position = "right")+
facet_grid(. ~ Assay, scales = "free")
任何人都可以帮助我将辅助轴放在图的顶部吗?
谢谢!!
我的MasterTable的输入如下:
结构(深度= c(15L,5L,2L,-1L,-3L,-5L,-7L,-9L,-11L,-13L,-15L,-17L,-19L,-21L,-23L, -25L,-27L,-29L,-31L,15L,5L,2L,-1L,-3L,-5L,-7L,-9L,-11L,-13L,-15L,-17L,-19L,-21L ,-23L,-25L,-27L,-29L,-31L),Conc.nM = c(24L,24L,24L,100L,100L,75L,75L,85L,85L,120L,300L,1000L,200L,240L ,240L,800L,1100L,1500L,2300L,0L,10L,0L,50L,200L,200L,50L,50L,200L,15L,0L,0L,10L,120L,200L,1500L,2100L,2000L,2000L),测定=结构(c(1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,2L, 2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L), . 标签= c(“仪器1”,“仪器2”),类=“factor”),Conc.uM = c(0L,0L,0L,1L,4L,10L,10L,10L,5L,7L,10L,14L,14L,14L,14L,13L,12L,12L,12L, 1L,1L,1L,4L,6L,9L,11L,11L,8L,8L,8L,20L,10L,9L,9L,9L,10L,10L,10L),可变=结构(c(2L,2L,2L) ,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,1L,1L,1L,1L,1L, 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L), . 标签= c(“碳”,“磷酸盐”),等级=“因子”)) , . Name = c(“Depth”,“Conc.nM”,“Assay”,“Conc.uM”,“Variable”),class =“data.frame”,row.names = c(NA,-38L) )
2 回答
感谢Brian的回答,并修改了上面提出的主题,我得到了下图 .
正如他所建议的那样,您必须首先使用以下内容手动修改数据:
然后,在代码中,使用与上面使用的校正因子相同的校正因子调整轴 . 这是制作图的代码 .
现在我还有一个问题需要解决 . 我想将刻度线和标签置于刻面之下 . 看起来比在图的顶部更合乎逻辑且更不繁忙 .
从你的代码:
考虑您希望"Carbon & Phosphate"与哪个主轴平行 . 还要考虑"x-axis"和"y-axis"在使用
coord_flip
的上下文中的含义 .TL;DR :只需将辅助轴移动到
scale_y_continuous
.