Skip to content

Commit

Permalink
bug fixing for grouped plots; fixed tidyselect expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
maraab23 committed Oct 29, 2024
1 parent 0e020c2 commit 43c8c3e
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 21 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: ggseqplot
Title: Render Sequence Plots using 'ggplot2'
Version: 0.8.4
Version: 0.8.5
Authors@R:
person("Marcel", "Raab", , "marcel.raab@gmail.com", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-3097-1591"))
Expand All @@ -20,7 +20,7 @@ Depends:
License: GPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
RdMacros: Rdpack
Imports:
cli,
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# ggseqplot 0.8.5

* fixed #5: if group vector is numeric, grouped plots are now sorted by number instead of order of appearance
* `.data` in `{tidyselect}` expressions was deprecated in `{tidyselect}` 1.2.0; update takes care of this change
* fixed incorrect group handling in `ggseqfplot` (issue Reported by Gilbert Ritschard)

# ggseqplot 0.8.4

* fixed #3: haven_labelled group vars are converted into factors before plotting
Expand Down
4 changes: 2 additions & 2 deletions R/ggseqdplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ ggseqdplot <- function(seqdata,
group <- forcats::fct_drop(group)
grinorder <- levels(group)
} else {
grinorder <- factor(unique(group))
grinorder <- factor(sort(unique(group)))
}

statefreqs <- purrr::map(
Expand Down Expand Up @@ -221,7 +221,7 @@ ggseqdplot <- function(seqdata,
names_transform = list(k = as.integer)
) |>
dplyr::mutate(k = factor(.data$k, labels = colnames(statefreqs)[-(1:2)])) |>
dplyr::mutate(x = factor(as.integer(.data$k)), .after = .data$k) |>
dplyr::mutate(x = factor(as.integer(.data$k)), .after = "k") |>
dplyr::full_join(grouplabspec)
)

Expand Down
4 changes: 2 additions & 2 deletions R/ggseqeplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ ggseqeplot <- function(seqdata,
) |>
dplyr::bind_rows() |>
dplyr::mutate(k = factor(.data$k, levels = unique(.data$k))) |>
dplyr::mutate(x = factor(as.integer(.data$k)), .after = .data$k) |>
dplyr::rename(entropy = .data$value) |>
dplyr::mutate(x = factor(as.integer(.data$k)), .after = "k") |>
dplyr::rename(entropy = "value") |>
dplyr::mutate(group = as.factor(group))


Expand Down
5 changes: 4 additions & 1 deletion R/ggseqfplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ ggseqfplot <- function(seqdata,
weighted = weighted,
idxs = ranks))

group <- rep(grinorder, each = max(ranks))
gr_length <- purrr::map(fplotdata, nrow) |>
unlist()

group <- rep(grinorder, gr_length)

coverage <- purrr::map(fplotdata,
~attributes(.x)$freq$Percent) |>
Expand Down
14 changes: 7 additions & 7 deletions R/ggseqiplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ ggseqiplot <- function(seqdata,
group <- forcats::fct_drop(group)
grinorder <- levels(group)
} else {
grinorder <- factor(unique(group))
grinorder <- factor(sort(unique(group)))
}
if (is.null(group)) grinorder <- factor(1)

Expand Down Expand Up @@ -198,7 +198,7 @@ ggseqiplot <- function(seqdata,
suppressMessages(
spelldata <- TraMineR::seqformat(seqdata, to = "SPELL") |>
dplyr::full_join(auxid, by = dplyr::join_by("id")) |>
dplyr::select(.data$idnew, dplyr::everything()) |>
dplyr::select("idnew", dplyr::everything()) |>
dplyr::mutate(
states = factor(.data$states,
levels = TraMineR::alphabet(seqdata),
Expand All @@ -212,20 +212,20 @@ ggseqiplot <- function(seqdata,
dplyr::group_by(.data$idnew) |>
dplyr::mutate(spell = dplyr::row_number(), .after = 1) |>
dplyr::as_tibble() |>
dplyr::rename(left = .data$begin, right = .data$end)
dplyr::rename(left = "begin", right = "end")
)


suppressMessages(
dt <- spelldata |>
dplyr::arrange(sortv) |>
dplyr::select(.data$idnew, .data$weight, group) |>
dplyr::select("idnew", "weight", group) |>
dplyr::distinct(.data$idnew, .keep_all = TRUE) |>
dplyr::ungroup() |>
dplyr::group_by(group) |>
dplyr::mutate(begin = 0, end = cumsum(.data$weight)) |>
dplyr::ungroup() |>
dplyr::select(-.data$weight, -.data$group) |>
dplyr::select(-"weight", -"group") |>
dplyr::full_join(spelldata, by = "idnew")
)

Expand Down Expand Up @@ -407,14 +407,14 @@ ggseqiplot <- function(seqdata,
aux = ifelse(.data$aux == 0, 1, .data$aux)
) |>
tidyr::uncount(.data$aux) |>
dplyr::select(-.data$aux) |>
dplyr::select(-"aux") |> #.data$aux
dplyr::group_by(.data$idnew) |>
dplyr::mutate(
left = dplyr::row_number() - 1,
right = ifelse(.data$aux2 == 0, .data$left, .data$left + 1)
) |>
dplyr::ungroup() |>
dplyr::select(-.data$aux2)
dplyr::select(-"aux2")


dt2 <- dt2 |>
Expand Down
2 changes: 1 addition & 1 deletion R/ggseqmsplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ ggseqmsplot <- function(seqdata,
group <- forcats::fct_drop(group)
grinorder <- levels(group)
} else {
grinorder <- factor(unique(group))
grinorder <- factor(sort(unique(group)))
}
if (is.null(group)) grinorder <- factor(1)

Expand Down
2 changes: 1 addition & 1 deletion R/ggseqmtplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ ggseqmtplot <- function(seqdata,
group <- forcats::fct_drop(group)
grinorder <- levels(group)
} else {
grinorder <- factor(unique(group))
grinorder <- factor(sort(unique(group)))
}

xandgrouplabs <- xandgrouplab(seqdata = seqdata,
Expand Down
6 changes: 3 additions & 3 deletions R/ggseqrfplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ provide a dissimilarity matrix ('diss')"
dotdata <- purrr::imap(seqrfobject$rf$dist.list,
~dplyr::tibble(k = .y, values = .x)) |>
dplyr::bind_rows() |>
dplyr::left_join(dplyr::select(boxdata, .data$k, .data$ymin, .data$ymax), by = "k") |>
dplyr::left_join(dplyr::select(boxdata, "k", "ymin", "ymax"), by = "k") |>
dplyr::filter(.data$values < .data$ymin | .data$values > .data$ymax)


Expand All @@ -350,8 +350,8 @@ provide a dissimilarity matrix ('diss')"
dplyr::group_by(.data$k) |>
dplyr::summarise(ymin = min(.data$aux_min),
ymax = max(.data$aux_max)) |>
dplyr::left_join(dplyr::select(boxdata, -c(.data$ymin,.data$ymax)), by = "k") |>
dplyr::relocate(.data$ymax, .after = .data$upper)
dplyr::left_join(dplyr::select(boxdata, -c("ymin","ymax")), by = "k") |>
dplyr::relocate("ymax", .after = "upper")


p2 <- ggplot() +
Expand Down
2 changes: 1 addition & 1 deletion R/ggseqrplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ ggseqrplot <- function(seqdata,
`Mean dist. to representative seq.` = .data$MD
) |>
dplyr::filter(.data$id != max(.data$id)) |>
tidyr::pivot_longer(cols = -.data$id))
tidyr::pivot_longer(cols = -"id"))


rplotdata <- purrr::imap(sort(unique(group)),
Expand Down
36 changes: 35 additions & 1 deletion R/ggseqtrplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,46 @@ ggseqtrplot <- function(seqdata,
group <- forcats::fct_drop(group)
grinorder <- levels(group)
} else {
grinorder <- factor(unique(group))
grinorder <- factor(sort(unique(group)))
}
if (is.null(group)) grinorder <- factor(1)

if (is.null(group)) group <- 1


if (dss == TRUE) {
aux <- purrr::map(grinorder,
~TraMineR::seqlength(TraMineR::seqdss(seqdata[group == .x,])) |>
max()
) |>
unlist() |>
min()
} else {
aux <- purrr::map(grinorder,
~TraMineR::seqlength(seqdata[group == .x,]) |>
max()
) |>
unlist() |>
min()
}

if (aux <= 1) {
if (dss == TRUE) {
cli::cli_abort(c(
"x" = "Cannot compute transitions rates for sequences if longest (group-specific) sequence length <= 1",
"i" = "consider using {.arg dss = FALSE} or different {.arg group} vector"
))
} else {
cli::cli_abort(c(
"x" = "Cannot compute transitions rates if longest (group-specific) sequence length <= 1",
"i" = "In case of active grouping, consider using different {.arg group} vector"
))
}
}




if (weighted == FALSE) {
weights <- 1
} else {
Expand Down

0 comments on commit 43c8c3e

Please sign in to comment.