krish
krish

Reputation: 1438

How to show calculated values on top of stacked bar chart in r

Using the code below, I have a stacked bar chart as shown.

myDF <- structure(list(Group = structure(1:3, .Label = c("2017-04-02", 
"2017-04-09", "2017-04-16"), class = "factor"), Passive = c(4, 
1, 0), Promoter = c(12, 1, 4), Detractors = c(0, 0, 0)), .Names = c("Group", 
"Passive", "Promoter", "Detractors"), row.names = c(NA, -3L), class = "data.frame")


x <- list(
  title = ""
)
y <- list(
  title = "Count"
)

p <- plot_ly(myDF, x = ~Group)

if ("Detractors" %in% colnames(myDF[-1])){
  p <- add_trace(p, y = ~`Detractors`, name = 'Detractors', type = 'bar',
                 marker = list(color = '#D52728')) #red
}
if ("Passive" %in% colnames(myDF[-1])){
  p <- add_trace(p, y = ~`Passive`, name = 'Passive', type = 'bar', 
                 marker = list(color = '#1F78B4')) #orange
}
if ("Promoter" %in% colnames(myDF[-1])){
  p <- add_trace(p, y = ~`Promoter`, name = 'Promoter', type = 'bar', 
                 marker = list(color = '#2BA02D')) #green
}
p <- layout(p, xaxis = x, yaxis = y, barmode = 'stack', legend = list(orientation = 'h'), showlegend=T)

p

enter image description here

I want to show the Net Promoter Scores on top of each bar. The formula I use for calculating the NPS is: (Number of Promoters — Number of Detractors) / (Number of Respondents) x 100. So for the first bar it would be ((12 - 0)/16) * 100 = 75. I want to show NPS: 75 on top of the first bar. Similarly for the second and third bar, the number on top will be (1-0)/2*100 = 50 and (4-0)/4*100 = 100 respectively. I will be getting more data for coming weeks, so there will be a bar for each week of data in the future. Is there a way to show this calculated value on top on the bars?

Upvotes: 4

Views: 4068

Answers (1)

Maximilian Peters
Maximilian Peters

Reputation: 31689

You could add annotations to your layout where the x values are your dates, the y values are the stacked values and the text is the Net Promoter Score, e.g.

enter image description here

df = data.frame(x = c('A', 'B', 'C', 'D'),
                y = c(1,3,2,4),
                calculated_values = c(20,30,10,50)
)
annotations <- list()
for (i in 1:length(df$calculated_values)) {
  annotations[[i]] <- list(x = df$x[[i]],
                           y = df$y[[i]],
                           text = df$calculated_values[[i]],
                           yanchor='bottom',
                           showarrow = FALSE)

}
plot_ly(df,
        x = ~x, 
        y = ~y, 
        type = 'bar') %>% 
  layout(annotations = annotations)

Or for this particular example replace the last two lines of your code with:

annotations <- list()
for (row in rownames(myDF)) {
  annotations[[as.integer(row)]] = list(x = as.character(myDF[row,]$Group),
                                y = sum(myDF[row,][-1]),
                                text = ((myDF[row,][[3]] - myDF[row,][[4]]) / sum(myDF[row,][-1])) * 100,
                                yanchor='bottom',
                                showarrow = FALSE)

}
p <- layout(p, xaxis = x, yaxis = y, barmode = 'stack', legend = list(orientation = 'h'), showlegend=T, 
            annotations = annotations)

p

enter image description here

Upvotes: 4

Related Questions