# spectrogram

    library(ggplot2)
    library(dplyr, warn.conflicts = F)
    library(retimer)
    data(mm1)

Various packages implement a spectrogram method including `seewave`, `tuneR`, `phonTools`, and `gsignal`. The problem is that they don’t return spectrograms in a standard format. With `retimer` you can use the `spectrogram()` wrapper to create a spectrogram with any of these packages and either return a `list` or a `tibble`.

For example, we can use the `tuneR::powerspec()` method like so:

    tuner_spec <- spectrogram(mm1, method = 'tuner', output = 'list')

and then plot it in base R:

    with(tuner_spec, image(time, freq, amp, col = hcl.colors(20)))

Or we could output as a `tibble` (the default output) like so:

    phontools_spec <- spectrogram(mm1, method = 'phontools')

and then plot it with `ggplot2`:

    phontools_spec |>
        ggplot() +
        geom_raster(aes(t, f, fill = amp), show.legend = F) +
        scale_fill_viridis_c()

We should get similar results with different methods:

    list(seewave = 'seewave', phontools = 'phontools', tuner = 'tuner', gsignal = 'gsignal') |>
        lapply(\(method) spectrogram(mm1, method = method)) |>
        bind_rows(.id = 'method') |>
        mutate(method = factor(method, levels = c('seewave', 'phontools', 'tuner', 'gsignal'))) |>
        group_by(method) |>
        mutate(amp = amp - min(amp),
               amp = amp / max(amp)) |>
        ungroup() |>
        ggplot() +
        geom_raster(aes(t, f, fill = amp), show.legend = F) +
        facet_grid(rows = vars(method)) +
        scale_fill_viridis_c()