Reputation: 97
I have this df:
df2 <- structure(list(group = c("Suicidio", "Soledad", "Preocupacion",
"Sin Amigos"), EMSE_2012 = c(6, 7.6, 6.5, -1.8),
EMSE_2018 = c(15.7,13.9, 10.5, -0.7)), class = "data.frame", row.names = c(NA, -4L))
and I need that each value of the bar is in the center of the bar:
p <- df2 %>%
pivot_longer(cols = -group) %>%
ggplot(aes(x = group, y = value, fill = name)) +
geom_col(position = 'dodge')
p + labs(title = "Diferencia de medias entre hombres y mujeres. Indicadores EMSE, 2012 y 2018",
y = "Prevalencia",fill = "Edición", x = "Factores de Riesgo de Salud Mental")
p + geom_text(aes(label = value), size = 4, hjust = 0, vjust = 0)
Thanks!
Upvotes: 0
Views: 408
Reputation:
Is this what you want?
library(dplyr)
library(ggplot2)
library(tidyr)
df2 <- structure(list(group = c("Suicidio", "Soledad", "Preocupacion",
"Sin Amigos"), EMSE_2012 = c(6, 7.6, 6.5, -1.8),
EMSE_2018 = c(15.7,13.9, 10.5, -0.7)), class = "data.frame", row.names = c(NA, -4L))
p <- df2 %>%
pivot_longer(cols = -group) %>%
ggplot(aes(x = group, y = value, fill = name)) +
geom_col(position = 'dodge')
p + labs(title = "Diferencia de medias entre hombres y mujeres. Indicadores EMSE, 2012 y 2018",
y = "Prevalencia",fill = "Edición", x = "Factores de Riesgo de Salud Mental")
p + geom_text(aes(label = value),
position = position_dodge(width=0.9))
Upvotes: 1