Double X axis on R studio ?

Hello,
I create a plot (using ggplot2) that deal with the density (y) in function of the years (x). Each year is associated with a certain depth. I want to have a visual representation of the link between years and depth. How can I do to add an axis, or rather a graduated scale with these depths below the years axis ? Thank you for your answer.
Here is an example with a fake dataset :

I wrote a package to do just this (among other things)! The tidypaleo package will let you create a dual age/depth axis in ggplot2 (although the axis will appear on the other side of the plot).

library(ggplot2)
# remotes::install_github("paleolimbot/tidypaleo")
library(tidypaleo) 

test_data <- tibble::tribble(
  ~depth, ~years, ~density,
      1,      5,        8,
      5,     10,       12,
     12,     15,       14,
     18,     20,       15
)

# create an age-depth model using tidypaleo::age_depth_model()
model <- age_depth_model(
  depth = test_data$depth,
  age = test_data$years
)

# use scale_x_age_depth() to create the second axis
ggplot(test_data, aes(x = years, y = density)) +
  geom_line() +
  scale_x_age_depth(model, depth_name = "depth")

Created on 2019-04-03 by the reprex package (v0.2.1)

It's possible to do this manually as well using sec_axis(), but it is complicated enough that using age_depth_model() is probably your best bet. Another option would be to use a geom_text() and/or geom_vline() to put the age and/or depth information on the plot directly.

5 Likes

Thank you so much for your help, I finally managed to make this graph.

I've just another question, in fact when I build my graph the age axis is reversed for exemple instead of having 2015 for a depth of 1 cm I have -3000. Do you have an explanation ? I don't know how only reversed "one part of the X graph" ... I even try to modify my dataset and to associated 1 cm with -3000 but the result was the same... Does anyone have the solution ?

Hi,

This would be easier for people to answer if you provided your code, plus example data that illustrates the problem, preferably as a REPRoducible EXample, eg using the reprex package.

Ron.

Hello,

data <- read.delim("data1.txt")

Here is my data set : 
data <- wrapr::build_frame(
  "depth"  , "years", "Concentration" |
    1L    , 2017L  , 3               |
    2L    , 1917L  , 4.5             |
    3L    , 1817L  , 6               |
    4L    , 1717L  , 7.5             |
    5L    , 1617L  , 9               |
    6L    , 1517L  , 10.5            |
    7L    , 1417L  , 12              |
    8L    , 1317L  , 13.5            |
    9L    , 1217L  , 15              |
    10L    , 1117L  , 16.5  

# create an age-depth model using tidypaleo::age_depth_model()
model <- age_depth_model(
  depth = data$depth,
  age = data$years
)

ggplot(data, aes(x = data$Concentration, y = data$years)) +
  geom_line() +
  scale_y_age_depth(model, depth_name = "depth")

I've try with the fonction reversed but that didn't work or I don't know how to apply it...

I couldn't run your code, I don't have wrapr installed and your code for that call isn't complete (it is at least missing a ")".

But there is an obvious error in your ggplot call, in that you shouldn't refer back to the data.frame when specifying columns in calls to aes, eg:

ggplot(data, aes(x = Concentration, y = years)) +
  geom_line() +
  scale_y_age_depth(model, depth_name = "depth")

Ron.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.