使用构面添加辅助X轴

时间:2017-08-02 18:22:48

标签: r ggplot2 axis facet

由于ggplot2.2.2 update不再适用this onethis one等旧解决方案,因此似乎未涵盖此主题。幸运的是,这个过程比以前简单得多。一行代码,您有一个辅助Y轴(as shown here)。

但我无法在我的情节上获得辅助X轴...

我正在比较沿着沉积物核心的金属浓度的深度剖面。我想将碳和磷酸盐浓度显示为金属浓度后的geom_area。问题是碳和磷酸盐浓度都与金属的浓度不同。因此我需要第二个轴。

主题如下(摘自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")

ggplot2 figure

任何人都可以帮助我将辅助轴放在图的顶部吗?

谢谢!

我的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(&#34;仪器1&#34;, &#34;乐器2&#34;),类=&#34;因子&#34;),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(&#34;碳&#34;,&#34;磷酸盐&#34;),类=&#34;因子&#34;)),. Name = c (&#34;深度&#34 ;, &#34; Concnn&#34;,&#34; Assay&#34;,&#34; Conc.uM&#34;,&#34; Variable&#34;),class =&#34; data.frame& #34;,row.names = c(NA, -38L))

2 个答案:

答案 0 :(得分:2)

感谢Brian的回答,并修改了上面提出的主题,我得到了下图。

Sediment core

正如他所建议的那样,您必须先使用以下内容手动修改数据:

MasterTable$Conc.uM <- MasterTable$Conc.uM *100

然后,在代码中,使用与上面使用的校正因子相同的校正因子调整轴。这是制作图的代码。

ggplot(MasterTable)+
  geom_line(aes(Depth,Conc.nM))+
  geom_area(aes(Depth,Conc.uM, fill=Variable), alpha=0.6)+                            #Area for second X-axis
  geom_area(aes(Depth,Conc.nM), alpha=0.95)+                                     
  geom_point(aes(Depth,Conc.uM), size=1, shape=16, alpha=0.3)+                        #Adding points for second X-axis
  geom_point(aes(Depth,Conc.nM), size=1, shape=16, alpha=0.8)+   
  scale_fill_manual(values=colours) + scale_colour_manual(values=colours) +      

  labs(title="Sediment core", color="",fill="")  +                                    #Place legend title in both color="" and fill=""

  scale_y_continuous("Metal concentration (nM)", 
                 sec.axis = sec_axis(~ . /100, name = "[Pi] (uM)                                             DOC (mg/L)"))+    
  scale_x_continuous("Depth (cm)", breaks=pretty_breaks(n=7))+

  coord_flip()+                                                                       #Required to make a proper depth profile 
  theme_new+                                                                          #Reference to custom theme
  facet_grid(. ~ Assay, scales = "free")                                              #Scales makes that the axis size can change 

现在我还有一个问题需要解决。我想将刻度线和标签置于刻面之下。似乎比在图的顶部更合乎逻辑且更不繁忙。

答案 1 :(得分:0)

从你的代码:

...
  scale_x_continuous("Depth (cm)", sec.axis = sec_axis(~ . *100, name = "Carbon & Phosphate"))+
  scale_y_continuous("Metal concentration (nM)") + 
  coord_flip() ...

考虑你想要哪个主轴&#34; Carbon&amp;磷酸盐&#34;平行于。还要考虑&#34; x轴&#34;和&#34; y轴&#34;表示在使用coord_flip

的上下文中

TL; DR :只需将辅助轴移至scale_y_continuous