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