最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plotly - A sunburst plot produced by R's plot_ly function shows a blank page - Stack Overflow

matteradmin5PV0评论

There is no way to produce a sunburst plot in R with this data using the plot_ly function from the plotly package.

library(plotly)

df <- data.frame(
   Valutazione = c('1 stella', '2 stelle', '3 stelle', '4 stelle', '5 stelle',
                   '1 stella', '2 stelle', '3 stelle', '4 stelle', '5 stelle',
                   '3 stelle', '4 stelle', '5 stelle'),
   Risposta = c('Negativa', 'Negativa', 'Negativa', 'Negativa', 'Negativa',
                'Neutra', 'Neutra', 'Neutra', 'Neutra', 'Neutra',
                'Positiva', 'Positiva', 'Positiva'),
   Numero = c(27, 21, 7, 1, 3, 2, 4, 16, 14, 8, 2, 31, 57)
)

plot <- plot_ly(
   data = df,
   parents = ~Risposta,
   labels = ~Valutazione,
   values = ~Numero,
   type = "sunburst",
   branchvalues="total") %>% layout(title = "Titolo")

plot

There is no way to produce a sunburst plot in R with this data using the plot_ly function from the plotly package.

library(plotly)

df <- data.frame(
   Valutazione = c('1 stella', '2 stelle', '3 stelle', '4 stelle', '5 stelle',
                   '1 stella', '2 stelle', '3 stelle', '4 stelle', '5 stelle',
                   '3 stelle', '4 stelle', '5 stelle'),
   Risposta = c('Negativa', 'Negativa', 'Negativa', 'Negativa', 'Negativa',
                'Neutra', 'Neutra', 'Neutra', 'Neutra', 'Neutra',
                'Positiva', 'Positiva', 'Positiva'),
   Numero = c(27, 21, 7, 1, 3, 2, 4, 16, 14, 8, 2, 31, 57)
)

plot <- plot_ly(
   data = df,
   parents = ~Risposta,
   labels = ~Valutazione,
   values = ~Numero,
   type = "sunburst",
   branchvalues="total") %>% layout(title = "Titolo")

plot
Share Improve this question asked Nov 17, 2024 at 0:20 Alfredo RoccatoAlfredo Roccato 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

To create a sunburst chart from your data you first have to rows containing the totals for each parent aka the three levels of Riposta. Additionally, as your labels= are not unique you have to use the ids= to get the correct grouping for which I add a helper column with a unique id.

library(plotly, warn = FALSE)
library(dplyr, warn = FALSE)

df <- df |>
  # Add a column of unique id's
  mutate(
    ids = paste(Valutazione, Risposta, sep = "_")
  )

# Add rows containing the totals
df1 <- df |>
  count(Risposta, wt = Numero, name = "Numero") |>
  rename(Valutazione = Risposta) |>
  mutate(ids = Valutazione, Risposta = "") |>
  bind_rows(df)

plot <- plot_ly(
  data = df1,
  parents = ~Risposta,
  labels = ~Valutazione,
  values = ~Numero,
  ids = ~ids,
  type = "sunburst",
  branchvalues = "total"
)

plot

Post a comment

comment list (0)

  1. No comments so far