I have the following data frame.
set.seed(2022)
df<-data.frame(edad=factor(rep(15:20, each=2)),
sexo=rep(c("F","M"),6),
valor=sample(10:20,12,T))
And I make the following graph.
library(ggplot2)
df %>%
ggplot(aes(x=edad, y=valor, fill=sexo))+
geom_col(position = position_dodge(1))+
geom_label(aes(label=valor), position = position_dodge(1), show.legend = F,
fill="white")
As you can see, as much as I have considered the position dodge
in the geom_label() I cannot get the labels to be separated.
How could I make it so that the labels are also in position dodge
? Keeping the fill blank ( fill="white"
).
Otherwise I consider the argument fill
, there if I get the labels in dodge
. As seen below.
That is, it should come out similar to the latter, but with the blank fill of the labels.
Pst: I also tried with the argument bg="white"
, but it still comes out the same.
The issue is that you are overwriting the dimension
fill
with which you lose the "adjustment"x
when positioning the labels. You can use the dimensiongroup
, with this you keep the color of the bars and the white in the labels, and the adjustment ofx
for these is done thanks togroup
:Result:
In the documentation:
We confirm that with
position_dodge()
we needgroup
and withposition_dodge2()
it is not necessary.