English 中文(简体)
Treemaps With Plotly: Blank Screen
原标题:
  • 时间:2023-06-17 20:39:42
  •  标签:
  • r
  • plotly

I am working with the R programming language.

I am trying to plot a Treemap with plotly, but it s returning a blank screen

I made my own dataset for this problem:

dados <- data.frame(
    Marca = c("Chevrolet", "Chevrolet", "Chevrolet", "Chevrolet", "Chevrolet", "Fiat", "Fiat", "Fiat", "Fiat", "Fiat", "Fiat", "Fiat", "Ford", "Ford", "Ford", "Ford", "Ford", "Ford", "Honda", "Honda", "Hyundai", "Hyundai", "JAC", "Renault", "Renault", "Renault", "Renault", "Renault", "Volkswagen", "Volkswagen", "Volkswagen", "Volkswagen", "Volkswagen", "Volkswagen"),
    Carro = c("celta", "fox", "montana", "onix", "s10", "agile", "bravo", "cruze", "punto", "siena", "strada", "uno", "ecosport", "focus", "idea", "ka", "palio", "ranger", "city", "fit", "hb20", "tucson", "j3", "clio", "duster", "fluence", "logan", "sandero", "crossfox", "gol", "jetta", "polo", "saveiro", "voyage"),
    Quantidade = c(483, 609, 119, 144, 117, 389, 137, 147, 397, 371, 146, 185, 93, 217, 240, 195, 134, 34, 234, 134, 149, 304, 92, 300, 186, 242, 439, 608, 132, 618, 130, 217, 75, 315)
)

treemap <- plot_ly(
    data = dados,
    type = "treemap",
    labels = ~Carro,
    parents = ~Marca,
    values = ~Quantidade
)

treemap

问题回答

You have to establish the parent labels; it s not inherent that it s the unique values of the data in Marca.

Here s how you get Plotly to interpret this correctly, start by creating a summary of the data to get parent container totals and parent container labels. Then you ll combine your original data with this summary. Check it out:

library(plotly)
library(tidyverse)

# summarize data frame to get parent container totals
d <- dados %>% group_by(Marca) %>% summarise(Quantidade = sum(Quantidade)) %>% 
  rename(Carro = Marca) %>%            # change columns
  mutate(Marca = "") %>%               # create blank column replacement
  select(names(dados))                 # put columns back in order

d2 <- rbind(d, dados) %>% as.data.frame() # combine with original data

Now, when you call your plot, you ll call the data d2. You need one more argument, as well: branchvalues.

treemap <- plot_ly(
  # data = dados,
  data = d2,
  type = "treemap",
  labels = ~Carro,
  parents = ~Marca,
  values = ~Quantidade,
  branchvalues = "total"
)

enter image description here

Here s all the code again, all together (easier copy + paste).

library(plotly)
library(tidyverse)

dados <- data.frame(
  Marca = c("Chevrolet", "Chevrolet", "Chevrolet", "Chevrolet", "Chevrolet", 
            "Fiat", "Fiat", "Fiat", "Fiat", "Fiat", "Fiat", "Fiat", "Ford",
            "Ford", "Ford", "Ford", "Ford", "Ford", "Honda", "Honda", "Hyundai", 
            "Hyundai", "JAC", "Renault", "Renault", "Renault", "Renault", 
            "Renault", "Volkswagen", "Volkswagen", "Volkswagen", "Volkswagen", 
            "Volkswagen", "Volkswagen"),
  Carro = c("celta", "fox", "montana", "onix", "s10", "agile", "bravo", "cruze",
            "punto", "siena", "strada", "uno", "ecosport", "focus", "idea", "ka", 
            "palio", "ranger", "city", "fit", "hb20", "tucson", "j3", "clio", 
            "duster", "fluence", "logan", "sandero", "crossfox", "gol", "jetta", 
            "polo", "saveiro", "voyage"),
  Quantidade = c(483, 609, 119, 144, 117, 389, 137, 147, 397, 371, 146, 185, 93,
                 217, 240, 195, 134, 34, 234, 134, 149, 304, 92, 300, 186, 242, 
                 439, 608, 132, 618, 130, 217, 75, 315)
)
# summarize data frame to get parent container totals
d <- dados %>% group_by(Marca) %>% summarise(Quantidade = sum(Quantidade)) %>% 
  rename(Carro = Marca) %>%            # change columns
  mutate(Marca = "") %>%               # create blank column replacement
  select(names(dados))                 # put columns back in order

d2 <- rbind(d, dados) %>% as.data.frame() # combine with original data

treemap <- plot_ly(
  # data = dados,
  data = d2, type = "treemap", labels = ~Carro,
  parents = ~Marca, values = ~Quantidade,
  branchvalues = "total"
)
treemap




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签