Observarun
Observarun

Reputation: 27

Plot wavelet coherence from sequences of numbers in R

I have multiple sequences of numbers in a text file, each made up of numbers 1 to 8. Numbers within the sequence are separated by a space, while each new sequence comes with a newline.

Data: The text file 'Sequences.txt' can be assumed to have the following 4 sequences (different lengths possible):

6 6 3 8 3 2 8 6 1 7 3 7 6 8 2 8 2 6 8 8 1 1 4 3 6 4 3 4 6 7

6 1 8 7 8 1 2 6 8 3 4 3 5 4 2 2 1 1 1 4 5 5 4 5 5 5 7 7 4 4 3 2 8 7 7

4 5 5 7 7 3 7 8 7 8 2 2 8 2 8 1 1 1 7 7 7 4 4 4

1 4 4 1 1 8 8 1 8 1 1 1 1 5 5 6 6 7 3 3 8 2 3 4 3 8 3 4 5 4 5 2 3 5 7

What I need is a plot of the kind shown in the picture next using R. Here, each sequence is arranged in a row and the sequences are stacked one below the other.File containing sequences

From literature, it looks like a wavelet plot, though I'm not certain.

Can really use help with this task (have tried a few packages, including biwavelet, sowas). Not showing my attempt here since I've already posted that in another question. Using R 4.2.1.

Upvotes: 0

Views: 125

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174468

There doesn't seem to be much of a pattern in the data, but you can extract and plot it as follows:

library(tidyverse)

readLines("Sequences.txt") %>%
  strsplit(" ") %>%
  lapply(as.numeric) %>%
  lapply(\(x) c(x, rep(NA, max(lengths(.)) - length(x)))) %>%
  as.data.frame() %>%
  setNames(seq_along(.)) %>%
  cbind(x = seq(nrow(.))) %>%
  pivot_longer(-x, names_to = "y", values_to = "z") %>%
  mutate(y = max(as.numeric(y)) - as.numeric(y) + 1) %>%
  ggplot(aes(x, y, fill = z)) +
  geom_raster() +
  scale_fill_distiller(palette = "Spectral", na.value = "white")

enter image description here

This image is essentially just showing you what the file looks like if you zoomed out and replaced each number with a color.

Upvotes: 1

Related Questions