I am trying to make the values of the column "activité en valeur" of my dataframe appear above my bars in geom_bar, yet it doesn't work as i get the following message of error "Aesthetics must be either length 1 or the same as the data (11): label". How can i fix it ?
library(ggplot2)
ggplot(fra_acti_valeur_par_type, aes(x = fra_acti_valeur_par_type$`type de séjour`, y =
fra_acti_valeur_par_type$`activité en valeur`,
fill = as.factor(fra_acti_valeur_par_type$annee)))
geom_bar(stat = "identity", position = "dodge")
ggtitle("Evolution du mix d'activité en valeur (CA/nombre de séjours)")
labs(y = "CA/nombre de séjours",
x = "type de séjour",
fill = "année")
geom_text(size = 1,
data = NULL,
position = position_stack(vjust = 0.9),
inherit.aes = TRUE,
label = fra_acti_valeur_par_type$`activité en valeur`
na.rm = TRUE)
Here is the structure of my data :
structure(list(`type de séjour` = c("Ambulatoires", "Externes",
"Hospitalisé", "Séances", "Ambulatoires", "Externes",
"Hospitalisé",
"Séances", "Ambulatoires", "Externes", "Hospitalisé", "Séances"
), `activité en valeur` = c(566.41149982681, 10.2857142857143,
2409.200042203, 197.406976744186, 623.226060993815, NaN,
2507.36267318663,
188.777777777778, 712.340679676985, 24, 2728.76832844575,
202.333333333333
), annee = c("2019", "2019", "2019", "2019", "2020", "2020",
"2020", "2020", "2021", "2021", "2021", "2021")), row.names = c(NA,
-12L), class = "data.frame")
CodePudding user response:
To add your labels map on the label
aesthetic inside aes()
instead of passing dataframe column as argument. Also, as you have a dodged bar you have to set the position for the labels to position_dodge()
as well, where you have to set the width of the dodge which by defaults to .9 for geom_bar/col
. I also rounded your values to one digit and moved them slightly by setting vjust=-.1
. Finally, as a general reminder, don't use fra_acti_valeur_par_type$...
in ggplot2. Simply map the column name on aesthetics:
library(ggplot2)
ggplot(fra_acti_valeur_par_type, aes(
x = `type de séjour`, y = `activité en valeur`,
fill = as.factor(annee)
))
geom_col(position = "dodge")
ggtitle("Evolution du mix d'activité en valeur (CA/nombre de séjours)")
labs(
y = "CA/nombre de séjours",
x = "type de séjour",
fill = "année"
)
geom_text(aes(label = round(`activité en valeur`, 1)),
vjust = -.1,
position = position_dodge(width = .9),
na.rm = TRUE
)
#> Warning: Removed 1 rows containing missing values (geom_col).
CodePudding user response:
Another option where you use group
in your geom_text
aesthetics, so it knows which values to dodge:
library(ggplot2)
ggplot(fra_acti_valeur_par_type, aes(x = `type de séjour`, y =
`activité en valeur`,
fill = as.factor(annee)))
geom_bar(stat = "identity", position = "dodge")
ggtitle("Evolution du mix d'activité en valeur (CA/nombre de séjours)")
labs(y = "CA/nombre de séjours",
x = "type de séjour",
fill = "année")
geom_text(data = fra_acti_valeur_par_type,
aes(x = `type de séjour`, y = `activité en valeur` 150, group = annee, label = format(`activité en valeur`, digits = 1)),
size = 3,
position = position_dodge(.9),
inherit.aes = TRUE,
na.rm = TRUE)
Output: