Skip to contents

Transforms a genotype frequency data frame from wide format (where genotype categories are columns) to long format, making it easier to use with ggplot2 and other tidyverse functions. Works well with freq for data visualization.

Usage

melt_freq(freq_df)

Arguments

freq_df

Usually the output from freq. A data frame where rows represent markers or individuals, and columns represent genotype categories with their frequencies.

Value

A data frame in long format with three columns:

  • "Marker": The marker or individual ID.

  • "Dosage": The genotype category.

  • "Frequency": The relative frequency of the genotype category.

Details

  • Uses tibble::rownames_to_column() to preserve marker or individual names.

  • Reshapes the data using pivot_longer() from tidyverse.

  • Ideal for visualization with ggplot2 or further data analysis on genotype frequencies by marker or by individual.

Examples

# Example frequency data frame
freq_data <- data.frame(
  `0` = c(0.2, 0.3, 0.4),
  `1` = c(0.5, 0.4, 0.4),
  `2` = c(0.3, 0.3, 0.2),
  row.names = c("Marker1", "Marker2", "Marker3")
)

# Convert to long format
melt_freq(freq_data)
#> # A tibble: 9 × 3
#>   Marker  Dosage Frequency
#>   <chr>   <chr>      <dbl>
#> 1 Marker1 X0           0.2
#> 2 Marker1 X1           0.5
#> 3 Marker1 X2           0.3
#> 4 Marker2 X0           0.3
#> 5 Marker2 X1           0.4
#> 6 Marker2 X2           0.3
#> 7 Marker3 X0           0.4
#> 8 Marker3 X1           0.4
#> 9 Marker3 X2           0.2