I'm making an interactive time series with Bar graphs using ggplot and after that converting it to plotly. Each bar represents one year 16,17 and 18 (data_long.edad) and I color them according to ocupation (data_long.ocup). My graph is showing count but I would like to be able to show the percentages for each bar (each bar should add up to 100%) I have tried many methods and failed. My problem is VERY similar to this question ggplot replace count with percentage in geom_bar but I cant make that solution work. I can´t transform my data (maybe because in my case I have factor variables?).
Any ideas?
> head(df,20)
data_long.edad data_long.ocup
1 a16r Oc. de limpieza
2 a18r Aún no ingesa al ML
3 a18r Aún no ingesa al ML
4 a17r Aún no ingesa al ML
5 a17r Aún no ingesa al ML
6 a16r Oficiales y operarios
7 a17r Aún no ingesa al ML
8 a17r Aún no ingesa al ML
9 a17r Trab. de los servicios
10 a16r Aún no ingesa al ML
11 a16r Trab. de los servicios
12 a16r Aún no ingesa al ML
13 a18r Administrativos
14 a18r Oficiales y operarios
15 a16r Trab. calificados
16 a18r Aún no ingesa al ML
17 a18r Aún no ingesa al ML
18 a18r Aún no ingesa al ML
19 a16r Oficiales y operarios
20 a16r Aún no ingesa al ML
gocup <- ggplot(df, aes(x=data_long.edad)) +
geom_bar(aes(fill=factor(data_long.ocup))) +
ggtitle("Trayectorias ocupacionales")
gocup
rm (ocuint)
ocupint = ggplotly(p= gocup, tooltip= c("x", "y")) %>%
style(hoverlabel = labels) %>%
layout(font = TRUE,
yaxis = list(fixedrange = TRUE),
showlegend = TRUE,
legend=list(title=list(text='<b> Ocupación </b>')),
xaxis = list(fixedrange = TRUE)) %>%
config(displayModeBar = FALSE)
ocupint
My graph looks like this, instead of count I would like percentage and each bar goes up to 100. (This graphs shows more years and more occupations than the data example I provided, but the same data structure)