axis with facets

Seems that this topic has not been covered this since the 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).

But I cannot get a secondary X-axis on my plots...

I am comparing a depth profile of metal concentrations along the sediment core. I would like to display carbon and phosphate concentrations as an geom_area behind the metal's concentration. The problem is that both carbon and phosphate concentrations are no on the same scale as the metal. Thus I need a second axis.

The theme is the following (taken from 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

And this code gives me a second Y-axis even though I specify it under X-axis.

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图

Can anyone help me place the secondary axis on the top of the figure?

Thanks!!

dput of my MasterTable is the following:

structure(list(Depth = 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), Assay = structure(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), Variable = structure(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))


From your code:

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

Consider which primary axis you want "Carbon & Phosphate" to be parallel to. Also consider what "x-axis" and "y-axis" mean in the context of using coord_flip .

TL;DR : Just move your secondary axis into scale_y_continuous .


Thanks to Brian's answer, and modifying the theme proposed above, I got the following figure.

泥沙核心

As he suggested, you have to first modify your data manually using something like this:

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

Then, in the code, adjust your axis with the same correction factor as the one used above. Here is the code to make the figure.

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 

Now I just have one problem left to solve. I would like for the tick marks and labels to be under the facet. Seems more logical and less busy than having it at the top of the figure.

链接地址: http://www.djcxy.com/p/64574.html

上一篇: R错误:“变量的无效类型(NULL)

下一篇: 轴与小平面