Values on top of bar graph using ggplot

Dear all,
I am using ggplot to get bar graphs for four different firm size classes for three different firm types (Manufacturing, Trading, Services) . On the x-axis I have firm size classes and the values for the different firm types on the y-axis. I want to get the exact value on top of each bar but I don't know how to do it. I always get the following error:
Error: geom_text requires the following missing aesthetics: x
Does anyone know how I can fix this and get the values on top of my bars?
Thank you very much in advance for your time and your help!

ggplot(average_taxgain_reshaped) +
  geom_col(aes(x=reorder(company_size, -value),
               y = value,
               fill = variable),
           position = "dodge") +
  geom_text(aes(y = value, label = value), vjust = 1.5) +
  ylab("") +
  xlab("") +
  scale_fill_manual(values = c(Trading = "salmon3", 
                               Manufacturing = "navajowhite1",
                               Services = "palegreen3"),
                    name = "Firm Type") +
  theme_light()

You are mapping the x-axis for geom_col()only so geom_text() doesn't inherit it, if you want to map it globally for your plot you have to do it inside the ggplot() function.

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Thanks for your quick answer and sorry for not providing an example before.
Please find below the data example with my code.



df <- data.frame (company_size  = c("VSC", "VSC", "VSC", "SC", "SC", "SC", "MC", "MC", "MC", "LC", "LC", "LC"),
                  variable = c("Trading", "Trading", "Trading", "Trading", "Manufacturing", "Manufacturing", "Manufacturing", "Manufacturing", "Services", "Services", "Services", "Services"),
                  value = c("40.8", "11.8", "7.5", "4.5", "35.7", "12.9", "8.4", "5.2", "41.3", "10.2", "8.4", "3.7"))

ggplot(average_taxgain_reshaped) +
  geom_col(aes(x=reorder(company_size, -value),
               y = value,
               fill = variable),
           position = "dodge") +
  geom_text(aes(y = value, label = value), vjust = 1.5) +
  ylab("") +
  xlab("") +
  scale_fill_manual(values = c(Trading = "salmon3", 
                               Manufacturing = "navajowhite1",
                               Services = "palegreen3"),
                    name = "Firm Type") +
  theme_light()

Is this what you want?

library(ggplot2)
library(dplyr)

average_taxgain_reshaped <- data.frame (company_size  = c("VSC", "VSC", "VSC", "SC", "SC", "SC", "MC", "MC", "MC", "LC", "LC", "LC"),
                  variable = c("Trading", "Trading", "Trading", "Trading", "Manufacturing", "Manufacturing", "Manufacturing", "Manufacturing", "Services", "Services", "Services", "Services"),
                  value = c("40.8", "11.8", "7.5", "4.5", "35.7", "12.9", "8.4", "5.2", "41.3", "10.2", "8.4", "3.7"))

average_taxgain_reshaped %>%
    group_by(company_size, variable) %>% 
    summarise(value = sum(as.numeric(value)), .groups = "drop") %>% 
    ggplot(aes(x = reorder(company_size, -value),
               y = value,
               fill = variable)) +
    geom_col(position = "dodge") +
    geom_text(aes(label = value), vjust = 1.5,
              position = position_dodge(width = 0.9)) +
    ylab("") +
    xlab("") +
    scale_fill_manual(values = c(Trading = "salmon3", 
                                 Manufacturing = "navajowhite1",
                                 Services = "palegreen3"),
                      name = "Firm Type") +
    theme_light()

Created on 2022-06-02 by the reprex package (v2.0.1)

yes, that's perfect! 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.