Inicio › Foros › Etapa I – Raíces Profundas › [AD] 2-3 – Tablas de Contingencia › Respuesta a: [AD] 2-3 – Tablas de Contingencia
Aquí la solución: https://stackoverflow.com/questions/60115375/adding-percentage-labels-to-a-barplot-with-y-axis-count-in-r
Aquí un ejemplo:
The issue is that ggplot doesn’t know that each facet is one group. This very useful tutorial helps with a nice solution. Just add aes(group = 1)
P.S. At the beginning, I was often quite reluctant and feared myself to manipulate my data and pre-calculate data frames for plotting. But there is no need to fret! It is actually often much easier (and safer!) to first shape / aggregate your data into the right form and then plot/ analyse the new data.
library(tidyverse)
library(scales)
ds <- mtcars ds$gear <- as.factor(ds$gear) First solution: ggplot(ds, aes(gear, fill = gear)) + geom_bar() + facet_grid(cols = vars(cyl), margins = T) + geom_text(aes(label = scales::percent(..prop..), group = 1), stat= "count")