# Stacked bar chart in loop

**URL:** <https://community.plotly.com/t/stacked-bar-chart-in-loop/4532>\
**Category:** Plotly R\
**Created:** [June 19, 2017, 12:03am UTC](https://community.plotly.com/t/stacked-bar-chart-in-loop/4532 "2017-06-19T00:03:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anamariamocanu](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@anamariamocanu](https://community.plotly.com/u/anamariamocanu)\
**Post date:** [June 19, 2017, 12:03am UTC](https://community.plotly.com/t/stacked-bar-chart-in-loop/4532/1 "2017-06-19T00:03:11Z")

</div>

Hi all,

I am trying to create stacked bar charts with annotations in a loop, as I have multiple columns to represent.  
It seems that the y value at add trace doesn’t change, but everything else does. I saw that one issue might be the evaluation, but this is not allowed in a bar object.

My code is:  
for (i in 1:n) {  
p \<- add\_trace(evaluation= TRUE, p, y = ~y2[i,], name = y2\_name[i],  
marker = list(color = y2\_color[i],  
line = list(color = y2\_color[i],  
width = 3)))

the issue is y2[i,] , which is 2 vectors merged through rbind.

Any thoughts?

Ana

---

<div class="post-metadata">

**Author:** ![franckvandersluis](https://avatars.discourse-cdn.com/v4/letter/f/0ea827/32.png) [@franckvandersluis](https://community.plotly.com/u/franckvandersluis)\
**Post date:** [June 27, 2017, 3:30pm UTC](https://community.plotly.com/t/stacked-bar-chart-in-loop/4532/2 "2017-06-27T15:30:13Z")

</div>

Hi Ana,

See below.

#First create the chart  
chart \<- plot\_ly(type = ‘bar’,  
hoverinfo = ‘x+y’)%\>%

#Then define the layout. You can’t do it after the loop  
layout(title = NULL,  
xaxis = list(title = x\_axis\_title, type = ‘categorical’, nticks = 10, categoryorder = ‘trace’),  
yaxis = list(title = ‘’),  
barmode = ‘stack’,  
paper\_bgcolor = white,  
plot\_bgcolor = white,  
font = font\_regular,  
margin = list(b = 100),  
showlegend = TRUE)

#then use the loop to add traces.  
for (i in 1:nrow(clientlist)){  
client \<- clientlist[i,]  
chart \<- add\_trace(chart,  
x = ~data$date,  
y = ~data[[client]],  
name = paste0(’**’,  
sum(data[[client]]), ’ (’, round(((100/total)\*sum(data[[client]])), 1),’%):**', gsub("\_", " ", client)), marker = list(color = pal[i]))  
}

return(chart)

variable clientlist is a list of client names. The data contains rows with aggregated data for each week. The columns contain the clients. The client list is used to look up a specific column in the data. So this example creates a stacked bar (of clients) per week.

---

<div class="post-metadata">

**Author:** ![anamariamocanu](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@anamariamocanu](https://community.plotly.com/u/anamariamocanu)\
**Post date:** [June 27, 2017, 10:55pm UTC](https://community.plotly.com/t/stacked-bar-chart-in-loop/4532/3 "2017-06-27T22:55:21Z")

</div>

Hi Franck,

Thanks a lot for sharing your code - it worked, I only needed to transpose my data and add the annotations.

Awesome work.

Cheers,  
Ana

---

<div class="post-metadata">

**Author:** ![franckvandersluis](https://avatars.discourse-cdn.com/v4/letter/f/0ea827/32.png) [@franckvandersluis](https://community.plotly.com/u/franckvandersluis)\
**Post date:** [June 28, 2017, 8:46am UTC](https://community.plotly.com/t/stacked-bar-chart-in-loop/4532/4 "2017-06-28T08:46:27Z")

</div>

Hi,

You’re welcome.

I have looked at your specific code better now. Removing the tilde would also do the job!

So: y = y2[i,]

Regards,  
Franck
