Struggling to create a grouped barchart with ggplot2

Hello everybody,

one again some difficulties bring me to this forum. I want to create a grouped barchart for the dataset below (Years as x) with ggplot 2. Sadly i just can´t get it correctly implemented in the abstract code.
Thank you in advance for any help!

data.frame(
stringsAsFactors = FALSE,
row.names = c("2017", "2018", "2019", "2020", "2021"),
FTMGMT = c(26, 28, 32, 29, 25),
PTMGMT = c(1, 1, 1, 1, 0),
FTEMPL = c(372, 413, 413, 430, 458),
PTEMPL = c(55, 46, 50, 53, 52),
FTWKR = c(1, 1, 4, 5, 6),
PTWKR = c(5, 6, 4, 5, 4),
Years = c("2017", "2018", "2019", "2020", "2021")
)

Hi @Abbath, see a example how to make this plot:

library(ggplot2)
library(tidyr)

# data frame
df <- data.frame(
  stringsAsFactors = FALSE,
  row.names = c("2017", "2018", "2019", "2020", "2021"),
  FTMGMT = c(26, 28, 32, 29, 25),
  PTMGMT = c(1, 1, 1, 1, 0),
  FTEMPL = c(372, 413, 413, 430, 458),
  PTEMPL = c(55, 46, 50, 53, 52),
  FTWKR = c(1, 1, 4, 5, 6),
  PTWKR = c(5, 6, 4, 5, 4),
  Years = c("2017", "2018", "2019", "2020", "2021")
)

# Reshape the data  to long format
df_long <- pivot_longer(df, -Years, names_to = "category", values_to = "value")

#  grouped barchart
ggplot(df_long, aes(x = Years, y = value, fill = category)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(x = "Year", y = "Value", fill = "Category") +
  ggtitle("Grouped Barchart") +
  theme_minimal()

Thank you so much for you help!!!!!

This topic was automatically closed 42 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.