1

I am making a stratigraphic plot but somehow, my data points don't connect correctly.

The purpose of this plot is that the values on the x-axis are connected so you get an overview of the change in d18O throughout time (age, ma).

I've used the following script:

library(readxl)
R_pliocene_tot <- read_excel("Desktop/R_d18o.xlsx")
View(R_pliocene_tot)
install.packages("analogue")
install.packages("gridExtra")

library(tidyverse)

R_pliocene_Rtot <- R_pliocene_tot %>%
  gather(key=param, value=value, -age_ma)

R_pliocene_Rtot

R_pliocene_Rtot %>%
  ggplot(aes(x=value, y=age_ma)) +
  geom_path() +
  geom_point() +
  facet_wrap(~param, scales = "free_x") +
  scale_y_reverse() +
  labs(x = NULL, y = "Age (ma)")

which leads to the following figure:

enter image description here

Something is wrong with the geom_path function, I guess, but I can't figure out what it is.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
MeiN
  • 11
  • 1
  • Welcome to SO! To help us to help you could you please make your issue reproducible by sharing a sample of your **data**? See [how to make a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Simply type `dput(NAME_OF_DATASET)` into the console and copy & paste the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do `dput(head(NAME_OF_DATASET, 20))` for the first twenty rows of data. – stefan Jan 19 '21 at 10:29
  • ... but you could try with adding the group aesthetic, i.e. add `group=1` inside `aes()` – stefan Jan 19 '21 at 10:30
  • Hi Stefan, Thanks for your reply! The problem is solved! (with group=1). Thanks a lot! – MeiN Jan 19 '21 at 10:42

1 Answers1

0

Though the comment seem solve the problem I don't think the question asked was answered. So here is some introduction about ggplot2 library regard geom_path

library(dplyr)
library(ggplot2)

# This dataset contain two group with random value for y and x run from 1->20
# The param is just to replicate the question param variable.
df <- tibble(x = rep(seq(1, 20, by = 1), 2),
             y = runif(40, min = 1, max = 100),
             group = c(rep("group 1", 20), rep("group 2", 20)),
             param = rep("a param", 40))

df %>%
  ggplot(aes(x = x, y = y)) +
  # In geom_path there is group aesthetics which help the function to know
  # which data point should is in which path.
  # The one in the same group will be connected together.
  # here I use the color to help distinct the path a bit more.
  geom_path(aes(group = group, color = group)) +
  geom_point() +
  facet_wrap(~param, scales = "free_x") +
  scale_y_reverse() +
  labs(x = NULL, y = "Age (ma)")

enter image description here

In your data which work well with group = 1 I guessed all data points belong to one group and you just want to draw a line connect all those data point. So take my data example above and draw with aesthetics group = 1, you can see the result that have two line similar to the above example but now the end point of group 1 is now connected with the starting point of group 2. So all data point is now on one path but the order of how they draw is depend on the order they appear in the data. (I keep the color just to help see it a bit clearer)

df %>%
  ggplot(aes(x = x, y = y)) +
  geom_path(aes(group = 1, color = group)) +
  geom_point() +
  facet_wrap(~param, scales = "free_x") +
  scale_y_reverse() +
  labs(x = NULL, y = "Age (ma)")

enter image description here

Hope this give you better understanding of ggplot2::geom_path

Sinh Nguyen
  • 4,277
  • 3
  • 18
  • 26