Getting total amount on geom bar

urtak %>%
  ggplot(aes(gerd,fill = litur_tveir)) + geom_bar(position = position_dodge2(padding = 0.05), alpha = 0.90, width = 0.75) +  theme_minimal() + theme(panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank(), panel.grid.major.y = element_line(color = 8), plot.title = element_text(hjust=0.5),plot.caption = element_textbox_simple(size = 12, linetype = 1, padding = margin(5,5,5,5))) + scale_fill_manual(values = c("Litur" = "Darkred", "Ekki" = "Grey")) + labs(x = "", y = "Fjöldi", fill = "", title = "Fjöldi eftir gerð og myndun litar", caption = "<b>Tafla 1</b><br>Fjöldi fiska eftir gerð og hvort þeir hafi myndað lit eða ekki, rauðu súlurnar eru fiskar sem hafa náð að mynda lit og gráu súlurnar eru fiskar sem ekki enn hafa myndað lit.")

This is my code and works great. My issue is I want to show the amount of fish in each catagory. Everytime I try geom text as the internet seems to say is the only way most other stuff gets ruined. Like the numbers on the side becomes two lines near the bottom with text Litur and Ekki. Same goes for getting it on the bar, all I get are the text saying the same thing.

Anyone with some way to get this working?

Thanks in advance

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Not sure how I can do that.
I am using 2 factors. Species and if they have produced color or not.
df %>%
ggplot(aes(species,fill = color)

  • geom_bar(position = position_dodge2(padding = 0.05), alpha = 0.90, width = 0.75)
  • theme_minimal()
  • theme(panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank(), panel.grid.major.y = element_line(color = 8), plot.title = element_text(hjust=0.5),plot.caption = element_textbox_simple(size = 12, linetype = 1, padding = margin(5,5,5,5)))
  • scale_fill_manual(values = c("Color" = "Darkred", "Colorless" = "Grey"))
  • labs(x = "", y = "Quantity", fill = "", title = "text", caption = "Picture 1
    text")

That iris dataframe seams to have numerical and only species as a factor

Use a sample of your own data instead, read the guide on the link I gave you before and try to provide a proper reproducible example so we can give you a working solution, otherwise, I can only offer you a generic answer.

Summarise your data frame before plotting to get the counts and use geom_col() instead of geom_bar() that way you can map the label aesthetic for geom_tex() to the previously calculated count.

library(tidyverse)
library(ggtext)
library(reprex)
library(cowplot)
library(knitr)
library(kableExtra)

urtak
data.frame(
gerd=c("Kuðungableikja", "Sílableikja", "Dvergableikja", "Sílableikja", "Dvergableikja", "Sílableikja", "Kuðungableikja", "Sílableikja", "Kuðungableikja","Kuðungableikja", "Sílableikja", "Dvergableikja", "Sílableikja", "Dvergableikja", "Sílableikja", "Kuðungableikja", "Sílableikja", "Kuðungableikja"),
litur_tveir=c("Litur","Ekki","Litur","Ekki","Ekki","Ekki","Litur","Litur","Ekki","Litur","Ekki","Ekki","Ekki","Litur","Ekki","Litur","Ekki","Litur")
)
urtak %>%
ggplot(aes(gerd,fill = litur_tveir)) + geom_bar(position = position_dodge2(padding = 0.05), alpha = 0.90, width = 0.75) + theme_minimal() + theme(panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank(), panel.grid.major.y = element_line(color = 8), plot.title = element_text(hjust=0.5),plot.caption = element_textbox_simple(size = 12, linetype = 1, padding = margin(5,5,5,5))) + scale_fill_manual(values = c("Litur" = "Darkred", "Ekki" = "Grey")) + labs(x = "", y = "Fjöldi", fill = "", title = "beautiful title", caption = "Tafla 1
nice litle caption")

So something like this?

Almost, a reprex should be minimal and you are including unrelated library calls and more code than needed but it is good enough.

I think this is what you are trying to do

library(tidyverse)

urtak <- data.frame(
  stringsAsFactors = FALSE,
              gerd = c("Kuðungableikja","Sílableikja",
                       "Dvergableikja","Sílableikja","Dvergableikja",
                       "Sílableikja","Kuðungableikja","Sílableikja",
                       "Kuðungableikja","Kuðungableikja","Sílableikja","Dvergableikja",
                       "Sílableikja","Dvergableikja","Sílableikja",
                       "Kuðungableikja","Sílableikja","Kuðungableikja"),
       litur_tveir = c("Litur","Ekki","Litur",
                       "Ekki","Ekki","Ekki","Litur","Litur","Ekki","Litur",
                       "Ekki","Ekki","Ekki","Litur","Ekki","Litur","Ekki",
                       "Litur")
)

urtak %>% 
    count(litur_tveir, gerd, name = "Fjöldi") %>% 
    ggplot(aes(x = gerd, y = Fjöldi, fill = litur_tveir)) +
    geom_col(position = position_dodge2(padding = 0.05), alpha = 0.90, width = 0.75) +
    geom_text(aes(label = Fjöldi),
              position = position_dodge2(width = 0.75),
              vjust = - 0.8) +
    theme_minimal() +
    theme(panel.grid.minor.x = element_blank(),
          panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_line(color = 8),
          plot.title = element_text(hjust=0.5),
          # plot.caption = element_textbox_simple(size = 12, linetype = 1, padding = margin(5,5,5,5))
    ) +
    coord_cartesian(clip = "off") +
    scale_fill_manual(values = c("Litur" = "Darkred", "Ekki" = "Grey")) +
    labs(x = "",
         y = "Fjöldi",
         fill = "",
         title = "beautiful title",
         caption = "Tafla 1 nice litle caption")

Created on 2022-03-10 by the reprex package (v2.0.1)

yes exactly thank you so much.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.