最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论