Commit ea323c7e authored by majpark21's avatar majpark21
Browse files

Validation: Fixed cluster color consistency through plots

parent 4d9b1cb5
...@@ -218,11 +218,12 @@ clustValid <- function(input, output, session, in.dataWide) { ...@@ -218,11 +218,12 @@ clustValid <- function(input, output, session, in.dataWide) {
return(NULL) return(NULL)
} }
return(factoextra::hcut(x = loc.dist, return(LOChcut(x = loc.dist,
k = returnNclust(), k = returnNclust(),
FUNcluster = "hclust", hc_func = "hclust",
hc_method = input$selectLinkage, hc_method = input$selectLinkage,
graph = FALSE)) hc_metric = input$selectDiss
))
}) })
# Plotting ---- # Plotting ----
...@@ -293,65 +294,81 @@ clustValid <- function(input, output, session, in.dataWide) { ...@@ -293,65 +294,81 @@ clustValid <- function(input, output, session, in.dataWide) {
return(loc.p) return(loc.p)
} }
# plot dendrogram tree # PCA visualization of partitioning methods
plotTree <- function() { plotClPCA <- function() {
cat(file = stderr(), 'plotTree: in\n') cat(file = stderr(), 'plotTree: in\n')
# make the f-n dependent on the button click # make the f-n dependent on the button click
locBut = input$butPlotInt locBut = input$butPlotInt
# Check if required data exists
# Thanks to isolate all mods in the left panel are delayed
# until clicking the Plot button # until clicking the Plot button
loc.part = calcDendCut() loc.part = calcDendCut()
loc.dm = in.dataWide()
print(sum(is.na(loc.dm)))
validate( validate(
need(!is.null(loc.part), "Nothing to plot. Load data first!") need(!is.null(loc.part), "Nothing to plot. Load data first!"),
need(!is.null(loc.dm), "Nothing to plot. Load data first!"),
need(sum(is.na(loc.dm)), "Cannot calculate PCA in the presence of missing data and/or NAs.")
) )
loc.p = factoextra::fviz_dend(loc.part, if (sum(is.na(loc.dm)) > 0)
show_labels = F, return(NULL)
rect = T,
xlab = "Time series", loc.p = factoextra::fviz_cluster(loc.part,
main = "Dendrogram") + data = loc.dm,
geom = "point",
elipse.type = "convex",
main = "Principal components"
)+
LOCggplotTheme(in.font.base = PLOTFONTBASE, LOCggplotTheme(in.font.base = PLOTFONTBASE,
in.font.axis.text = PLOTFONTAXISTEXT, in.font.axis.text = PLOTFONTAXISTEXT,
in.font.axis.title = PLOTFONTAXISTITLE, in.font.axis.title = PLOTFONTAXISTITLE,
in.font.strip = PLOTFONTFACETSTRIP, in.font.strip = PLOTFONTFACETSTRIP,
in.font.legend = PLOTFONTLEGEND) in.font.legend = PLOTFONTLEGEND)
return(loc.p)
# Retrieve association of cluster and colours and use it for dendrogram for color matching between dend, silhouette and PCA plot
temp = ggplot_build(loc.p)
map_individual = as.data.table(temp$data[[1]][, c("colour", "shape")])
map_cluster = map_individual[, .SD[1], by = shape]
map_cluster[, cluster := 1:nrow(map_cluster)]
return(list(plot = loc.p, mapping_individual = map_individual, mapping_cluster = map_cluster))
} }
# PCA visualization of partitioning methods # plot dendrogram tree
plotClPCA <- function() { plotTree <- function() {
cat(file = stderr(), 'plotTree: in\n') cat(file = stderr(), 'plotTree: in\n')
# make the f-n dependent on the button click # make the f-n dependent on the button click
locBut = input$butPlotInt locBut = input$butPlotInt
# Check if required data exists # Check if required data exists
# Thanks to isolate all mods in the left panel are delayed
# until clicking the Plot button
loc.part = calcDendCut() loc.part = calcDendCut()
loc.dm = in.dataWide() # Rerun the PCA plot to obtain clour mapping of clusters in PCA and silhouette plot and match it with dendrogram colors.
print(sum(is.na(loc.dm))) loc.map = plotClPCA()
validate( validate(
need(!is.null(loc.part), "Nothing to plot. Load data first!"), need(!is.null(loc.part), "Nothing to plot. Load data first!"),
need(!is.null(loc.dm), "Nothing to plot. Load data first!"), need(!is.null(loc.map), "Nothing to plot. Load data first!")
need(sum(is.na(loc.dm)), "Cannot calculate PCA in the presence of missing data and/or NAs.")
) )
if (sum(is.na(loc.dm)) > 0) # Determine cluster order of occurence from left to right in the dendrogram
return(NULL) # This is necessary because fviz_dend colors clusters from left to right,
# whereas fviz_silhouette and fviz_cluster use the order of cluster first occurence in the list of individuals.
loc.mapClus = loc.map$mapping_cluster
ord.clusDend = unique(loc.part$cluster[loc.part$order])
col.clusDend = loc.mapClus[, colour][ord.clusDend]
loc.p = factoextra::fviz_cluster(loc.part, loc.p = factoextra::fviz_dend(loc.part,
data = loc.dm, k = returnNclust(),
geom = "point", k_colors = col.clusDend,
elipse.type = "convex", show_labels = F,
main = "Principal components" rect = T,
)+ xlab = "Time series",
main = "Dendrogram") +
LOCggplotTheme(in.font.base = PLOTFONTBASE, LOCggplotTheme(in.font.base = PLOTFONTBASE,
in.font.axis.text = PLOTFONTAXISTEXT, in.font.axis.text = PLOTFONTAXISTEXT,
in.font.axis.title = PLOTFONTAXISTITLE, in.font.axis.title = PLOTFONTAXISTITLE,
...@@ -361,15 +378,14 @@ clustValid <- function(input, output, session, in.dataWide) { ...@@ -361,15 +378,14 @@ clustValid <- function(input, output, session, in.dataWide) {
return(loc.p) return(loc.p)
} }
# plot silhouetts for a particular dendrogram cut
# plot silhouettes for a particular dendrogram cut
plotSilhForCut <- function() { plotSilhForCut <- function() {
cat(file = stderr(), 'plotSilhForCut: in\n') cat(file = stderr(), 'plotSilhForCut: in\n')
# make the f-n dependent on the button click # make the f-n dependent on the button click
locBut = input$butPlotInt locBut = input$butPlotInt
# Check if required data exists
# Thanks to isolate all mods in the left panel are delayed
# until clicking the Plot button # until clicking the Plot button
loc.part = calcDendCut() loc.part = calcDendCut()
validate( validate(
...@@ -421,6 +437,7 @@ clustValid <- function(input, output, session, in.dataWide) { ...@@ -421,6 +437,7 @@ clustValid <- function(input, output, session, in.dataWide) {
# pdf(NULL) # pdf(NULL)
loc.p = plotClPCA() loc.p = plotClPCA()
loc.p = loc.p$plot
if(is.null(loc.p)) if(is.null(loc.p))
return(NULL) return(NULL)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment