admin管理员组文章数量:1435740
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
1 Answer
Reset to default 0To 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
本文标签: plotlyA sunburst plot produced by R39s plotly function shows a blank pageStack Overflow
版权声明:本文标题:plotly - A sunburst plot produced by R's plot_ly function shows a blank page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745641251a2667871.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论