-1

I'm trying to plot a population pyramid with a text box in front of each bar (according to gender and age)

My dataset looks like this:

plotstack <- structure(list(`Grupos de edad` = c(
  "0 a 14", "0 a 14", "0 a 14",
  "0 a 14", "0 a 14", "0 a 14", "0 a 14", "0 a 14", "15 a 29",
  "15 a 29"
), Depto = c(
  "Antioquia", "Atlántico", "Bogotá", "Valle del Cauca",
  "Antioquia", "Atlántico", "Bogotá", "Valle del Cauca", "Antioquia",
  "Atlántico"
), variable = c(
  "Hombres", "Hombres", "Hombres", "Hombres",
  "Mujeres", "Mujeres", "Mujeres", "Mujeres", "Hombres", "Hombres"
), total_edad = c(
  721865L, 345326L, 747582L, 528768L, -689789L,
  -332823L, -719228L, -513970L, 845150L, 348966L
), freq_total = c(
  10.8,
  12.7, 9.65, 11.7, -10.3, -12.2, -9.29, -11.3, 12.7, 12.8
), n = c(
  6677930L,
  2722128L, 7743955L, 4532152L, 6677930L, 2722128L, 7743955L, 4532152L,
  6677930L, 2722128L
)), row.names = c(NA, -10L), class = "data.frame")

and the code I'm using to plot is

library(ggplot2)
library(dplyr)
plotstack%>%
  ggplot(plotstack,mapping = aes(x =freq_total/100, 
                     y = `Grupos de edad`, fill = variable)) +
  geom_col() +
  scale_x_continuous(labels =abs
                     , limits = max(edadesplot$freq_total/100) * c(-1,1)) +
  labs(x = "Porcentaje de población")+labs(title='Nacional')+
  theme(legend.title = element_blank())+ 
  geom_label(aes(label = paste(abs(freq_total)," %", sep=""), group = factor(variable)), 
             fill='white', colour = "black", 
             position= position_dodge(0), 
             size = 3)+
  scale_fill_brewer(palette = "Set1") + 
  theme_bw()+theme(legend.title = element_blank())+ facet_grid(. ~ Depto)

But my plot ends up looking like this: enter image description here

as you can see, when the bars are too short, the boxes overlap as they don't have enough space, I'd like to know how can I move them so they don't overlap, maybe putting all the boxes on the same x value.

stefan
  • 90,330
  • 6
  • 25
  • 51
JeffCJ
  • 15
  • 4
  • 1
    Does this answer your question? [Dynamic position for ggplot2 objects (especially geom\_text)?](https://stackoverflow.com/questions/6996538/dynamic-position-for-ggplot2-objects-especially-geom-text) – BrianLang Sep 30 '20 at 07:33

1 Answers1

0

ggrepel was made for this.

library(ggrepel)
library(ggplot2)
library(dplyr)
plotstack%>%
  ggplot(plotstack,mapping = aes(x =freq_total/100, 
                     y = `Grupos de edad`, fill = variable)) +
  geom_col()+
  labs(x = "Porcentaje de población")+labs(title='Nacional')+
  theme(legend.title = element_blank())+ 
  ggrepel::geom_label_repel(aes(label = paste(abs(freq_total)," %", sep=""), group = factor(variable)), 
             fill='white', colour = "black", 
             position= position_dodge(0), 
             size = 3)+
  scale_fill_brewer(palette = "Set1") + 
  theme_bw()+theme(legend.title = element_blank())+ facet_grid(. ~ Depto)

The plot that you make with this code

BrianLang
  • 831
  • 4
  • 14