1

I have this data frame:

  District `2011` `2016` `2021`
  <chr>     <dbl>  <dbl>  <dbl>
1 YTM       44045  49046  44458
2 KC        37660  41802  40994

df <- tibble(District = c("YTM", "KC"),
       `2011` = c(44045, 37660),
       `2016` = c(49046, 41802),
       `2021` = c(44458, 40994))

and I hope to plot a bar chart with x as district and need to group by years.

the result is similar to this

enter image description here

1 Answer 1

1

Here you go:

library(tidyverse)

df %>% 
  pivot_longer(-District) %>% 
  ggplot(aes(x = name, y = value, fill = District)) +
  geom_col(position = position_dodge())+
  scale_fill_manual(values = c("red", "blue"))+
  labs(title = "Number of Things per Month", x="Year", "Value")+
  theme_minimal()+
  theme(legend.position = "bottom")

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.