Problem with point-labels while overlapping geom_point and geom_bar in ggplot

Viewed 38

I'm working on a overlapped graph, with bars measuring income in y-axis and points measuring gender gap in secondary y axis

My specific problem is that I can't get point labels close to those. Rather they seem to follow the position of the center bar label corresponding to each year.

He's the code:

graf1 %>%
  mutate(Sexo = fct_relevel(Sexo,"Mujeres", "Total", "Hombres")) %>% 
ggplot(aes(fill=Sexo, y=ing_medio, x=Year)) + 
  geom_bar(position="dodge", stat="identity")+
  coord_cartesian(ylim = c(0,800))+
  geom_text(aes(label=paste0("$",round(ing_medio,1))),
            vjust=-0.5,size=3,check_overlap = F,
            position = position_dodge(0.9))+
  scale_fill_brewer(palette="Paired")+
  geom_line(aes( y=brecha, x=Year, group=1,color ="Brecha de género (%)"), inherit.aes = FALSE)+
  scale_color_manual(NULL, values = "red") +
  geom_point(aes( y=brecha, x=Year, group=1,color ="Brecha de género (%)"), inherit.aes = FALSE)+
  geom_text(aes(label=(ifelse(Sexo=="Total",paste0("-",brechap),""))))+
  scale_x_discrete("Año")+
  scale_y_continuous(name="Ingreso medio en miles de pesos ($)",
                     sec.axis = sec_axis( trans=~./2000, 
                     name="Brecha (%)",
                     labels = scales::label_percent(),
                     breaks = seq(0, 1, by = 0.05)))+
  theme(panel.grid.minor.y = element_line(color="lightgrey"),
        panel.grid.major.y = element_line(color="lightgrey"),
        element_blank(),panel.background = element_blank(),legend.position="bottom",
        legend.text=element_text(size=8),
        plot.title = element_text(face = "bold",size=9, hjust=0.5),
        axis.title=element_text(size=8,face="bold"),
        axis.title.y.right = element_text(angle = 90))+
  labs(title="Ingreso medio mensual de las personas ocupadas y brecha de género en el ingreso, según sexo y año, 2017-2021")
  

And here's an image

Overlapped graph, problem with point-labels

Thank you!

1 Answers

Thank you guys!

It works perfectly at adding y=brecha inside the aes of the second geom_text

Related