From my understanding of your code for plotting one pareto chart, you are isolating a single year and then plot the cumulative sum expressed as a percentage.
So, if you want to do that for multiple years and multiple columns, you need first to pivot your datatable into a longer format (here I'm using pivot_longer
from tidyr
but you can do the same thing using melt
from data.table
).
Then, I will group your data by Year and by the categorical variable "Var" (containing Col1, Col2, ...) and expressed the value as a percentage of the total and the cumulative sum in a percentage. I also create a count which is basically row numbers in order to use it as x axis.
Finally, I used these new variables to make the barchart and the line. I separated "Years" using facet_wrap
. Altogether, you can write something like that:
dt %>% pivot_longer(., -YEAR, names_to = "Var", values_to = "Val") %>%
group_by(YEAR, Var) %>%
arrange(desc(Val), .by_group = TRUE) %>%
mutate(CumS = cumsum(Val), Count = row_number()) %>%
mutate(CumS2 = CumS*100 / sum(Val)) %>%
mutate(Val_Percent = Val*100/sum(Val)) %>%
ggplot(aes(x = as.factor(Count), y = Val_Percent, fill = Var))+
geom_col(position = position_dodge())+
facet_wrap(.~YEAR)+
geom_line(aes(y = CumS2, group = Var, color = Var), position = position_dodge(.9))+
theme(axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.ticks.x = element_blank())

Alternatively, if you want get a pareto chart per "Col" and per "Year", you can do the exact same thing and then use of facet_grid
instead of facet_wrap
:
dt %>% pivot_longer(., -YEAR, names_to = "Var", values_to = "Val") %>%
group_by(YEAR, Var) %>%
arrange(desc(Val), .by_group = TRUE) %>%
mutate(CumS = cumsum(Val), Count = row_number()) %>%
mutate(CumS2 = CumS*100 / sum(Val)) %>%
mutate(Val_Percent = Val*100/sum(Val)) %>%
ggplot(aes(x = as.factor(Count), y = Val_Percent, fill = Var))+
geom_col(position = position_dodge())+
facet_grid(Var~YEAR)+
geom_line(aes(y = CumS2, group = Var, color = Var), position = position_dodge(.9))+
theme(axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.ticks.x = element_blank())

Does it answer your question ?