Stacked Barchart with total amount of bars text
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- R
- Sterne
- 2.7k
- Forks
- 641
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
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')

Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginnen Sie mit dem reproduzierbaren R-Beispiel unter Verwendung von data.table::melt, plot_ly und add_text und untersuchen Sie anschließend, wie gestapelte Balken-Traces die Beschriftungen der Summen positionieren. Als abgeschlossen gilt die Anzeige der Summe oberhalb jedes gestapelten Balkens auf new_frame, ohne das data.table-Objekt zu casten.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- plotly, r
- Bereich
- data-visualization
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100