2

I used application called 'displayr' to create a sankey digaram and it will generate the chart based on the spreadsheet you upload. However, I want to sort the right part of the nodes to make it more visual. It will be best to sort the data by DESC use R code. I am trying to find the useful code and the position to put into my current code to make it happen.

Here are my chart and the R code automatically generated by the software. enter image description here

library(flipPlots) library(flipData) library(flipFormat) library(flipChartBasics)

weights <- NULL
dat <- NULL
dat <- get0("formTable")
if (is.null(dat))
{
    if (exists("formEnteredData") && sum(dim(formEnteredData)) > 0)
        dat <- flipTransformations::ParseEnteredData(formEnteredData)
}

if (is.null(dat))
{
    dat <- as.data.frame(get0("formVariables"))
    if (is.null(dat) || sum(dim(dat)) == 0)
        stop("No data has been provided.")
    weights <- QPopulationWeight
    names(dat) <- if (!isTRUE(get0("formNames"))) Labels(formVariables) else Names(formVariables)
}

if (isTRUE(get0("formContainsWeights")))
{
    weights <- dat[,ncol(dat)]
    dat <- dat[,-ncol(dat)]
}
if (formTidyLabels)
    names(dat) <- ExtractCommonPrefix(names(dat))$shortened.labels
dat <- TidyRawData(dat, weights = weights, subset = QFilter, missing = "Use partial data", error.if.insufficient.obs = FALSE)

sankey.dat <- SankeyDiagram(dat, max.categories = formMaxCategories, link.color = formLinkColors,
                        subset = TRUE, weights = attr(dat, "weights"),
                        variables.share.values = get0("formSharedValues", ifnotfound=FALSE),
                        hovertext.show.percentages = formHoverPercentages,
                        label.show.counts = formShowCounts, label.show.percentages = formShowPercentages,
                        label.show.varname = formShowVar, label.max.length = formLabelMaxLen,
                        output.data.only = TRUE)
num.colors <- length(unique(sankey.dat$nodes$group))
u.ind <- which(!duplicated(sankey.dat$nodes$group, fromLast = formLinkColors %in% c("Target", "Last variable")))
u.names <- sankey.dat$nodes$name[u.ind]

# Different from 'Named colors' in other charts because nodes can be merged
# And questions names/counts/percentages may be included in node name
# we use partial matching.
template <- get0("formTemplate")
if (!is.null(template))
{
    bcol <- rep(NA, num.colors)
    names(bcol) <- u.names
    col.list <- if (!is.null(template$brand.colors)) template$brand.colors else template$colors
    col.names <- names(col.list)
    col.ord <- order(nchar(col.names))
    for (i in col.ord)
    {
        ind <- grep(paste0("\\Q", col.names[i], "\\E"), u.names)
        if (length(ind) > 0)
           bcol[ind] <- col.list[i]
    }
    template$brand.colors <- bcol
}

colors <- NULL
if (formPalette != "Legacy colors")
    colors <- ChartColors(num.colors, given.colors = GetPalette(formPalette, template),
                custom.color = formCustomColor,
                custom.gradient.start = formCustomGradientStart,
                custom.gradient.end = formCustomGradientEnd,
                custom.palette = formCustomPalette, silent = TRUE) 


if (is.null(template))
    template <- list(global.font = list(family = "Arial", color = "#2C2C2C", 
        size = 7.5, units = "pt"), fonts = list(`Values axis title` = list(
        family = "Arial", color = "#2C2C2C", size = 9)))                           


                                 
sankey <- SankeyDiagram(links.and.nodes = sankey.dat, colors = colors, sinks.right = formNodeRight,
                          font.family = get0("formFontFamily", ifnotfound = template$fonts$`Values axis title`$family), 
                          font.size = get0("formFontSize", ifnotfound = template$fonts$`Values axis title`$size),
                          font.unit = get0("formFontUnit", ifnotfound = template$global.font$units), 
                          node.width = formNodeWidth, node.padding = formNodePad)
严蓉冰
  • 53
  • 3

0 Answers0