0

enter image description here

I want to make the above graph in plotly where men and women variables are stacked together with the year as the x axis?

camille
  • 16,432
  • 18
  • 38
  • 60
Raisham
  • 47
  • 6
  • [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, all necessary code, and a clear explanation of what you're trying to do and what hasn't worked. – camille Dec 15 '19 at 01:10

1 Answers1

0

Maybe try something like below?

df = data.frame(
Year=rep(1999:2019,2),
Percent=rpois(42,4),
Sex=rep(c("Men","Women"),each=21))

library(plotly)
COLS = c("#ffd369","#015668")
df$Year = factor(df$Year)

side by side:

p = plot_ly(df,x=~Year,y=~Percent,name=~Sex,type='bar',colors=COLS,color=~Sex) %>%
layout(xaxis = list(title =NA),
legend = list(orientation = "h",xanchor = "center",x=0.5)
)

stacked:

p %>% layout(barmode = 'stack')

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72