dissolveGroups.Rd
This function dissolves specified groups in a SPATA2
object by merging them into
the closest neighboring groups based on the pairwise distances
between observations.
dissolveGroups(object, grouping, groups_dissolve, grouping_new = NULL)
An object of class SPATA2
or, in case of S4 generics,
objects of classes for which a method has been defined.
Character value. The grouping variable of interest. Use
getGroupingOptions()
to obtain all valid input options.
A character vector specifying the names of the groups to be dissolved.
A character string specifying the name for the new grouping variable.
If NULL
, the original grouping variable will be updated. Default is NULL
!
The updated input object, containing the added, removed or computed results.
This function performs the following steps:
Retrieves the metadata data frame from the SPATA2
object.
Checks if the specified grouping and groups to dissolve exist in the object.
Computes the pairwise distances between all observations.
Identifies the closest neighboring groups for the observations in the groups to be dissolved.
Updates the grouping variable with the new group assignments.
If grouping_new
is provided, a new grouping variable is created; otherwise, the original grouping variable is updated.
library(SPATA2)
library(ggplot2)
library(patchwork)
object <- loadExampleObject("UKF313T")
# add example grouping
# this is a random grouping variable solely created for demonstrating the
# purpose of dissolveGroups()! It is not of any analytical value!
object <- addFeatures(object, feature_df = example_data$dissolve_groups)
# note the many spots of class 'unnamed' surrounded by actual groups
plot_before <-
plotSurface(object, color_by = "histo_bad", pt_clrp = "uc", clrp_adjust = c("unnamed" = "black"))
# show plot
plot_before
# dissolve the group "unnamed"
object <-
dissolveGroups(
object = object,
grouping = "histo_bad",
groups_dissolve = "unnamed",
grouping_new = "histo_better"
)
# spots of group 'unnamed' have been dissolved into their respective neighbor group
# use alpha (transparency) to highlight spots that used to of group "unnamed"
plot_afterwards <-
plotSurface(object, color_by = "histo_better", pt_clrp = "uc", alpha_by = "histo_alpha") +
scale_alpha_identity()
# show plots
plot_before + plot_afterwards