尝试使用 R 对 Sankey 图中的节点进行排序?

问题描述

我使用名为“displayr”的应用程序创建了一个 sankey digaram,它会根据您上传的电子表格生成图表。但是,我想对节点的正确部分进行排序,使其更加直观。最好使用 R 代码按 DESC 对数据进行排序。我正在尝试找到有用的代码以及将其放入当前代码中的位置以实现它。

这是我的图表和软件自动生成的 R 代码

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",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)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...