Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

geom_text position and paste %

Here is my dataframe despesa_anys_ESP_decilsllar:

structure(list(Any = c("2019", "2019", "2019", "2019", "2019", 
"2019", "2019", "2019", "2019", "2019", "2020", "2020", "2020", 
"2020", "2020", "2020", "2020", "2020", "2020", "2020"), decils_renda = c("1r", 
"2n", "3r", "4t", "5è", "6è", "7è", "8è", "9è", "10è", 
"1r", "2n", "3r", "4t", "5è", "6è", "7è", "8è", "9è", "10è"
), despesatotal = c(13866.4, 18324.6, 21495.5, 24455.3, 28460.8, 
29818, 33897, 37897.3, 43373.1, 57458.4, 13280.3, 17337.7, 20666.9, 
22900.4, 25972.2, 27152.6, 30469.6, 32036.5, 37087.2, 49570.1
), despesamonetaria = c(9493.2, 13622.9, 16488.8, 19245.1, 22622.4, 
23780.2, 27213.5, 30793.5, 35457.3, 46773.7, 9006.1, 12629.2, 
15337.3, 17209.8, 19882.8, 20736.3, 23711.3, 24780.6, 28962.6, 
38315.8), proporcio_despesatotal = c(45.9, 60.6, 71.1, 80.9, 
94.1, 98.6, 112.1, 125.3, 143.4, 190, 49.2, 64.2, 76.6, 84.8, 
96.2, 100.6, 112.9, 118.7, 137.4, 183.6), proporcio_despesamonetaria = c(39.6, 
56.8, 68.8, 80.3, 94.4, 99.2, 113.5, 128.4, 147.9, 195.1, 43.9, 
61.5, 74.7, 83.9, 96.9, 101, 115.5, 120.8, 141.1, 186.7), despesatotal_gr = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, -4.2, -5.4, -3.9, -6.4, -8.7, 
-8.9, -10.1, -15.5, -14.5, -13.7), despesamonetaria_gr = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, -5.1, -7.3, -7, -10.6, -12.1, 
-12.8, -12.9, -19.5, -18.3, -18.1)), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame"))

And I want to add to my plot geom_text, being the label despesamonetaria_gr (for all Anys values, except 2019, plus adding % to the label.

So far I did:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

plot_despesa_decils_ESP <- ggplot(despesa_anys_ESP_decilsllar, aes(x = decils_renda, y = despesamonetaria, fill = Any)) +
  geom_bar(position="dodge", stat="identity", width=0.8) +
  scale_y_continuous("", labels=function(x) format(x, big.mark = ".", scientific = FALSE), limits=c(0, 50000), breaks=seq(from=0, to=50000, by=5000),
                     expand = expansion(mult = c(0, 0))) +
  geom_text(aes(label = paste(ifelse(!Any == "2019", despesamonetaria_gr, ""), "%"), family = "serif", size=6.5, hjust = -1.5, vjust = -2),
             show.legend = FALSE)

But the labels are not positioned correctly to their corresponding year:

enter image description here

>Solution :

As a general rule when adding labels to bars you have to account for the position in geom_text too, i.e. add position = position_dodge(width = .9) so that the labels are dodged in line with the bars. For the issue with the % I simply switched to scales::percent.

library(ggplot2)

ggplot(despesa_anys_ESP_decilsllar, aes(x = decils_renda, y = despesamonetaria, fill = Any)) +
  geom_bar(position = "dodge", stat = "identity", width = 0.8) +
  scale_y_continuous("",
    labels = function(x) format(x, big.mark = ".", scientific = FALSE),
    limits = c(0, 50000), breaks = seq(from = 0, to = 50000, by = 5000),
    expand = expansion(mult = c(0, 0))
  ) +
  geom_text(
    aes(
      label = ifelse(!Any == "2019", scales::percent(despesamonetaria_gr, scale = 1), "")
    ),
    position = position_dodge(width = .9),
    family = "serif", size = 3, vjust = -.2,
    show.legend = FALSE
  )

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading