Stacked Barchart with total amount of bars text
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- R
- Estrellas
- 2.7k
- Forks
- 641
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
Problem: I have a stacked barchart. Above each of the stacked bars I want to have a total number (i.e. the sum of the two bars - here: total). I tried to take usage of add_text but it did not work out as expected. Any suggestions what to change?
Remark: I do not want to cast the data.table object. The target frame on which the visualizations shall be made is new_frame.
Many thanks
Brief description of the problem
library(data.table)
library(plotly)
Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.table(Animals, SF_Zoo, LA_Zoo)
new_frame <- data.table::melt(data, id.vars='Animals')
new_frame[, total := sum(value), by = Animals]
plot_ly(new_frame, x = ~Animals,
y = ~value,
type = 'bar',
name = ~variable,
text = ~paste(variable, value, total),
textposition = 'auto',
# insidetextfont = list(size = 10, color = 'black'),
hovertext = ~paste(
"<b>Hover text works IN/OUT</b><br>"),
hoverinfo = 'text',
hoverlabel = list(align = "left"),
showlegend = TRUE) %>%
add_text(
visible = "legendonly",
x= ~Animals,
y = ~total,
# showlegend = TRUE,
name = "Titel",
cliponaxis = FALSE,
textfont = list(size = 10)
)%>%
layout(yaxis = list(title = 'Count'), barmode = 'stack')

Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con el ejemplo reproducible de R usando data.table::melt, plot_ly y add_text; después, inspecciona cómo los trazos de barras apiladas posicionan las etiquetas del total. Se considera terminado cuando se muestre la suma encima de cada barra apilada en new_frame sin hacer casting del objeto data.table.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- plotly, r
- Área
- data-visualization
- Tipo de issue
- Error
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 42/100