添加带有小平面的辅助 X 轴

Add secondary X-axis with facets

自从 ggplot2.2.2 update where old solutions like this one and this one no longer apply. Fortunately, the process is far simpler than before. One line of code and you have a secondary Y-axis (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")

谁能帮我把副轴放在图的顶部?

谢谢!!

我的 MasterTable 的输出如下:

结构(列表(深度= c(15L,5L,2L,-1L,-3L,-5L,-7L,-9L, -11L, -13L, -15L, -17L, -19L, -21L, -23L, -25L, -27L, -29L, -31L, 15 升、5 升、2 升、-1 升、-3 升、-5 升、-7 升、-9 升、-11 升、-13 升、-15 升、-17 升、 -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), .Label = c("Instrument 1", "Instrument 2"), class = "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), .Label = c("Carbon", "Phosphate"), class = "factor")), .Names = c("Depth", "Conc.nM", "Assay", "Conc.uM", "Variable"), class = "data.frame", row.names = c(NA, -38L))

来自您的代码:

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

考虑您希望 "Carbon & Phosphate" 平行于哪个主轴。还要考虑 "x-axis" 和 "y-axis" 在使用 coord_flip.

的上下文中的含义

TL;DR:只需将您的辅助轴移动到 scale_y_continuous

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

正如他所建议的,您必须先使用如下方式手动修改数据:

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 

现在我只剩下一个问题要解决了。我希望刻度线和标签位于刻面下方。看起来比将它放在图的顶部更合乎逻辑且不那么忙。