2

I have a data frame with two columns x's and y's. Each row represents a line and in each cell is a list with 51 consecutive observations (so 2 lists in each row for x and y).

I want to fill the space between the lines in the data frame.

The problem is that there's a randomness in x and y, so I can't just take the ymin and ymax for each data point on x.

This code would create sample data (with only 2 lines of 10 observations each) that is similar to my actual dataset:

library(data.table)

genData <- function() {
  set.seed(42)
  genOneLine <- function(m_x, m_y) {
    xs = seq(0,1,by=0.1)
    x_ran <- rnorm(8, m_x, 0.1)
    xs[2:9] = xs[2:9] + x_ran
    ys = seq(0,1,by=0.1)
    y_ran <- rnorm(8, m_y, 0.1)
    ys[2:9] = ys[2:9] + y_ran
    return (data.table(x = list(xs), y = list(ys)))
  }
  return (rbind(genOneLine(-0.1, -0.1), genOneLine(0.1, 0.1)))
}
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Leander
  • 1,322
  • 4
  • 16
  • 31
  • 5
    You may not be able to share your real data, but surely you can throw together some *example* data? Say, ~10 rows with similar structure and properties as your real data to demonstrate the problem? [See here for advice on creating reproducible examples](https://stackoverflow.com/q/5963269/903061). Use `set.seed` to make any randomness reproducible. – Gregor Thomas Nov 13 '18 at 20:22
  • What do you mean "fill the space between the lines in the data frame"? Do you mean something about plotting the data or interpolating? – G5W Nov 13 '18 at 20:26
  • sure, I can throw together some example data! I thought it was more or less clear, but it probalby helps. I'll update the question in a few minutes – Leander Nov 13 '18 at 20:44
  • I have updated the question – Leander Nov 13 '18 at 21:05

1 Answers1

1

See if this is what you have in mind?

library(dplyr)
library(ggplot2)
library(data.table)

df <- genData()

df %>%

  # add identifier variable to each line
  mutate(id = seq(1, n())) %>%

  # make each element of the list its own row
  tidyr::unnest() %>%

  # add sequence identifier, from start of line 1 to its end, then
  # from the end of line 2 to its start
  group_by(id) %>%
  mutate(order = ifelse(id == 1, seq(1, n()), n() + seq(n(), 1))) %>%
  ungroup() %>%

  # sort data frame
  arrange(order) %>%

  # plot
  ggplot(aes(x = x, y = y)) +
  geom_polygon(aes(group = 1), fill = "grey20", alpha = 0.5) +
  geom_path(aes(color = factor(id)), size = 2)

result

Z.Lin
  • 28,055
  • 6
  • 54
  • 94