-2

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() +
geom_bar
  • 1
    It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. If you want to post your data type `dput(NAME_OF_DATASET)` into the console and copy the output starting with `structure(....` into your post. – stefan Jul 04 '22 at 08:39
  • Just a reminder of the [previous advice](https://stackoverflow.com/questions/72745803/r-panel-data-model-visualisation) to make your question [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). If you don't it is very difficult for anyone to help. – SamR Jul 04 '22 at 08:40
  • @stefan 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") – user19304348 Jul 04 '22 at 09:02

2 Answers2

2

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).

stefan
  • 90,330
  • 6
  • 25
  • 51
1

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:

enter image description here

Quinten
  • 35,235
  • 5
  • 20
  • 53
  • it works, thanks ! tho i do not understand the "y + 150" in the aesthetics argument ? – user19304348 Jul 04 '22 at 12:21
  • No problem @user19304348. That is to adjust the position of the text. If you want to have the labels higher or lower, you can change the 150 so that it will be displayed closer or higher to the bars. – Quinten Jul 04 '22 at 12:23
  • Hi @user19304348, how can I help you further to use `group` in `geom_text`? – Quinten Jul 21 '22 at 10:11