0

I can't figure out how to solve this.

The code at the end of the post produces this plot:

enter image description here

How can I make it so that each year has one column per product group (food & tobacco, personal care, etc...)? That is 5 columns per year.

Many thanks!

library(janitor)
library(tidyverse)
library(dplyr)

# Format data

us_exp <- clean_names(USPersonalExpenditure)

us_exp <- USPersonalExpenditure %>% 
  as.data.frame() %>% 
  rownames_to_column(., "type")

us_exp <- us_exp %>% 
  pivot_longer(!type, names_to = "year", values_to = "count") %>% 
  as_tibble()

# ggplot
  
ggplot(us_exp) +
  aes(x = year,
      y = count,
      group = type,
      fill = type) +
  geom_col()
  theme_classic(base_family = "helvetica_regular") +
  theme(legend.position="bottom",
        text = element_text(size = 24)) +
  scale_fill_npg() +
  ggtitle("...") +
  xlab(NULL) +
  ylab(NULL)
Tomas R
  • 440
  • 2
  • 10

1 Answers1

1

Use position = "dodge" or position = position_dodge() for additional arguments.

ggplot(us_exp) +
  aes(x = year, y = count, group = type, fill = type) +
  geom_col(position = "dodge")

enter image description here

Maël
  • 45,206
  • 3
  • 29
  • 67