Leander
Leander

Reputation: 1395

fill area between two lines with different x-values

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

Upvotes: 2

Views: 177

Answers (1)

Z.Lin
Z.Lin

Reputation: 29085

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

Upvotes: 1

Related Questions