TransWikia.com

hctreemap2 de dos niveles --> Error: Can't subset columns that don't exist

Stack Overflow en español Asked by Alejandro Erazo Bolaños on November 7, 2021

He estado intentando hacer un treemap de dos niveles en hctreemap2 y encontré un ejemplo en este enlace https://rpubs.com/jbkunst/hctreemap2

hctreemap2 (data = GNI2014,
           group_vars = c ("continente", "iso3"),
           size_var = "población",
           color_var = "GNI",
           layoutAlgorithm = "squarified",
           levelIsConstant = FALSE,
           niveles = lista (
             list (nivel = 1, dataLabels = list (enabled = TRUE)),
             list (nivel = 2, dataLabels = list (enabled = FALSE)),
             list (nivel = 3, dataLabels = list (habilitado = FALSE))
           ))%>%
  hc_colorAxis (minColor = brewer.pal (7, "Verdes") [1],
               maxColor = brewer.pal (7, "Verdes") [7])%>%
  hc_tooltip (pointFormat = "<b> {point.name} </b>: <br>
                            Pop: {point.value:,. 0f} <br>
                            GNI: {point.colorValue:,. 0f} ")

Pero cuando intento ejecutarlo aparece un mensaje de error "Error: Can’t subset columns that don’t exist. x Column iso3 doesn’t exist". Pero la columna sí existe en el dataframe. Lo mismo ocurre con mis datos, simplemente reemplazo los datos del código con los nombres de mis datos y aparece el mismo error: "Error: Can’t subset columns that don’t exist. x Column objeto_contratar doesn’t exist" . Pero la columna objeto_contratar sí existe. No sé si para este tipo de gráficos los datos deben tener un formato específico o si estoy pasando algo por alto.

Cuando corro traceback() esto es lo que me muestra:

>   traceback()
31: stop(fallback)
30: signal_abort(cnd)
29: abort(bullets, class = "dplyr_error", error_name = error_name, 
        error_expression = error_expression, index = .index, dots = dots, 
        fn = fn, parent = parent)
28: stop_dplyr(i, dots, fn = "summarise", problem = conditionMessage(e), 
        parent = e)
27: value[[3L]](cond)
26: tryCatchOne(expr, names, parentenv, handlers[[1L]])
25: tryCatchList(expr, classes, parentenv, handlers)
24: tryCatch({
        for (i in seq_along(dots)) {
            quo <- dots[[i]]
            chunks[[i]] <- mask$eval_all_summarise(quo)
            mask$across_cache_reset()
            result_type <- types[[i]] <- tryCatch(vec_ptype_common(!!!chunks[[i]]), 
                vctrs_error_incompatible_type = function(cnd) {
                    abort(class = "dplyr:::error_summarise_incompatible_combine", 
                      parent = cnd)
                })
            if ((is.null(dots_names) || dots_names[i] == "") && is.data.frame(result_type)) {
                map2(seq_along(result_type), names(result_type), 
                    function(j, nm) {
                      mask$add(nm, pluck(chunks[[i]], j))
                    })
            }
            else {
                mask$add(auto_named_dots[i], chunks[[i]])
            }
        }
        recycle_info <- .Call(dplyr_summarise_recycle_chunks, chunks, 
            mask$get_rows(), types)
        chunks <- recycle_info$chunks
        sizes <- recycle_info$sizes
        for (i in seq_along(dots)) {
            result <- vec_c(!!!chunks[[i]], .ptype = types[[i]])
            if ((is.null(dots_names) || dots_names[i] == "") && is.data.frame(result)) {
                cols[names(result)] <- result
            }
            else {
                cols[[auto_named_dots[i]]] <- result
            }
        }
    }, error = function(e) {
        if (inherits(e, "rlang_error_data_pronoun_not_found")) {
            stop_error_data_pronoun_not_found(conditionMessage(e), 
                index = i, dots = dots, fn = "summarise")
        }
        else if (inherits(e, "dplyr:::error_summarise_incompatible_combine")) {
            stop_combine(e$parent, index = i, dots = dots, fn = "summarise")
        }
        else if (inherits(e, "dplyr:::summarise_unsupported_type")) {
            stop_summarise_unsupported_type(result = e$result, index = i, 
                dots = dots)
        }
        else if (inherits(e, "dplyr:::summarise_incompatible_size")) {
            stop_summarise_incompatible_size(size = e$size, group = e$group, 
                index = e$index, expected_size = e$expected_size, 
                dots = dots)
        }
        else {
            stop_dplyr(i, dots, fn = "summarise", problem = conditionMessage(e), 
                parent = e)
        }
    })
23: summarise_cols(.data, ...)
22: summarise.grouped_df(., value = sum(!!size_sym), colorValue = sum(!!color_sym))
21: summarise(., value = sum(!!size_sym), colorValue = sum(!!color_sym))
20: function_list[[i]](value)
19: freduce(value, `_function_list`)
18: `_fseq`(`_lhs`)
17: eval(quote(`_fseq`(`_lhs`)), env, env)
16: eval(quote(`_fseq`(`_lhs`)), env, env)
15: withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
14: data %>% group_by(!!!group_syms[1:depth]) %>% summarise(value = sum(!!size_sym), 
        colorValue = sum(!!color_sym)) %>% ungroup() %>% mutate(name = !!group_syms[[depth]], 
        level = depth) %>% mutate_at(group_vars, as.character()) %>% 
        {
            if (depth == 1) 
                mutate(., id = paste0(name, 1))
            else {
                mutate(., parent = pmap_chr(list(!!!group_syms[1:depth - 
                    1]), name_cell, depth = depth - 1), id = paste0(parent, 
                    name, depth))
            }
        }
13: .f(.x[[i]], ...)
12: map(., data_at_depth)
11: function_list[[i]](value)
10: freduce(value, `_function_list`)
9: `_fseq`(`_lhs`)
8: eval(quote(`_fseq`(`_lhs`)), env, env)
7: eval(quote(`_fseq`(`_lhs`)), env, env)
6: withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
5: 1:length(group_vars) %>% map(data_at_depth) %>% bind_rows()
4: hctreemap2(data = Objeto, group_vars = c("nombre_grupo", "objeto_contratar"), 
       size_var = "total", color_var = "propcant", layoutAlgorithm = "squarified", 
       levelIsConstant = FALSE, levels = list(list(level = 1, dataLabels = list(enabled = TRUE)), 
           list(level = 2, dataLabels = list(enabled = FALSE)), 
           list(level = 3, dataLabels = list(enabled = FALSE))))
3: eval(lhs, parent, parent)
2: eval(lhs, parent, parent)
1: hctreemap2(data = Objeto, group_vars = c("nombre_grupo", "objeto_contratar"), 
       size_var = "total", color_var = "propcant", layoutAlgorithm = "squarified", 
       levelIsConstant = FALSE, levels = list(list(level = 1, dataLabels = list(enabled = TRUE)), 
           list(level = 2, dataLabels = list(enabled = FALSE)), 
           list(level = 3, dataLabels = list(enabled = FALSE)))) %>% 
       hc_colorAxis(minColor = brewer.pal(7, "Greens")[1], maxColor = brewer.pal(7, 
           "Greens")[7]) %>% hc_tooltip(pointFormat = "<b>{point.name}</b>:<br>n                            Cantidad: {point.value:,.0f}<br>n                            Propoción: {point.colorValue:,.0f}")

One Answer

Por lo que se puede ver en el github del proyecto, hay un bug abierto al respecto. Puedes intentar usar la forma deprecada, mediante treemap:

treemap(GNI2014,
        index = c("continent", "country"),
        vSize = "population",
        vColor = "GNI",
        type = "value",
        draw = FALSE) %>% 
  hctreemap(allowDrillToNode = TRUE, layoutAlgorithm = "squarified") %>% 
  hc_tooltip(pointFormat = "<b>{point.name}</b>:<br>
                            Pop: {point.value:,.0f}<br>
                            GNI: {point.valuecolor:,.0f}")

Aunque entiendo que lo resultados no son los mismos, o bien, intentar con la versión de desarrollo que por lo que veo, incorpora un "fix" con respecto a este error:

devtools::install_github("jbkunst/highcharter")

Answered by Patricio Moracho on November 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP