1

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 figure

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

Marty999
  • 213
  • 1
  • 4
  • 12

2 Answers2

3

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

Sediment core

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.

Marty999
  • 213
  • 1
  • 4
  • 12
0

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.

Brian
  • 7,900
  • 1
  • 27
  • 41
  • Hi Brian, thank you for this. good catch, it now displays the x-axis on top. But the data is not following this axis. It is still linked to the bottom x-axis. Is there a way to specify that I would like Conc.uM data to follow sec_axis? – Marty999 Aug 02 '17 at 19:47
  • The data are the data, they don't "follow" a particular axis. They should be pre-transformed to a common scale (e.g. multiply by 1000 to put your micromoles in nanomoles); then your second axis simply *relabels* them. Can't do much more to help without at least a subset of your data. – Brian Aug 02 '17 at 19:52
  • Yeah, that's the way I saw it as well. Here is my data file: https://www.dropbox.com/s/9t1ppyfvm983try/LAS2016_Master-table8.csv?dl=0 – Marty999 Aug 02 '17 at 20:02
  • Please put the output from `dput(MasterTable)` in the text of your question. Many SO users won't have access to dropbox (or your link may die eventually). – Brian Aug 02 '17 at 20:04
  • Thanks Brian, pre-adjusting them to the same scale worked! I will post the results for others shortly. – Marty999 Aug 02 '17 at 22:29