Nevedha Ayyanar
Nevedha Ayyanar

Reputation: 865

How to display the total value at the top of the stacked bar chart in R

I'm having a data frame from which the stacked bar chart is plotted in R

myDF <- data.frame("group" = c("A","B","C","A","B","C"),
                    "date" = c("25-May","25-May","25-May","01-Jun","01-Jun","01-Jun"),
                     "hours" = c(42,48,51,32,25,48),
                      stringsAsFactors = FALSE)

p <- plot_ly(myDF, x = myDF$date,
        y = myDF$group,
        type = 'bar',
        name = myDF$group,
        text = myDF$Hours,
        color = myDF$group,
        colors = brewer.pal(length(unique(myDF$group)),
                            "Paired"))%>%
  layout(barmode = 'stack',hoverlabel = list(bgcolor= 'white') ,bargap = 0.5) %>%
  layout(xaxis = list(categoryorder = 'array',
                      categoryarray = myDF$date), showlegend = F)

I'm having another data frame where I have total hours in the week as follows:

totalHours <- data.frame(
                          "date" = c("25-May","01-Jun"),
                           "hours" = c(141,105))

I'm trying to display the sum of stacks at the top of the bar like this

annotations <- list()
for (i in 1:length(totalHours$hours)) {
  annotations[[i]] <- list(x = totalHours$date[[i]],
                          text = totalHours$hours[[i]],
                          yanchor='bottom',
                          showarrow = FALSE)
}

Added annotations in the layout of the plot

p <- p %>% layout(annotations = annotations)

enter image description here

I am getting the label at the center than being displayed at the top. I am not clear where there is a miss in the code. Can anyone provide a solution for this?

Thanks in advance!!

Upvotes: 0

Views: 757

Answers (1)

Duck
Duck

Reputation: 39605

You can try this. It is an issue with position of labels:

library(plotly)
library(RColorBrewer)
#Data
myDF <- data.frame("group" = c("A","B","C","A","B","C"),
                   "date" = c("25-May","25-May","25-May","01-Jun","01-Jun","01-Jun"),
                   "hours" = c(42,48,51,32,25,48),
                   stringsAsFactors = FALSE)
#Plot
p <- plot_ly(myDF, x = myDF$date,
             y = myDF$group,
             type = 'bar',
             name = myDF$group,
             text = myDF$Hours,
             color = myDF$group,
             colors = brewer.pal(length(unique(myDF$group)),
                                 "Paired"))%>%
  layout(barmode = 'stack',hoverlabel = list(bgcolor= 'white') ,bargap = 0.5) %>%
  layout(xaxis = list(categoryorder = 'array',
                      categoryarray = myDF$date), showlegend = F)
#Data2
totalHours <- data.frame(
  "date" = c("25-May","01-Jun"),
  "hours" = c(141,105),stringsAsFactors = F)

#Create global coordinate
labs <- myDF %>% group_by(group) %>% mutate(Tot = sum(hours),Prop=hours/Tot) %>% 
  ungroup() %>% summarise(Val=sum(Prop))
#Annotate
annotations <- list()
for (i in 1:length(totalHours$hours)) {
  annotations[[i]] <- list(x = totalHours$date[[i]],
                           y = labs$Val,
                           text = totalHours$hours[[i]],
                           yanchor='bottom',
                           showarrow = FALSE)
}
#Final plot
p <- p %>% layout(annotations = annotations)

Output:

enter image description here

Upvotes: 1

Related Questions