temo
temo

Reputation: 69

Data wrangling for creating multiple bar graph

So, I have this tibble from which I am trying to make a multiple bar graph that shows how much was spent supporting(for) or opposing(against) each of these candidates

However, I am completely lost on how to go about doing it, and I think I want to rearrange this tibble to make it simpler to create a graph. Any pointers would be very helpful.

A tibble: 5 x 5
  type                   clinton      sanders     omalley fa_camp
  <chr>                  <dbl>         <dbl>         <dbl> <chr>   
1 24A                 51937848        859337             0 against 
2 24C                 15106530           900             0 for     
3 24E                 29651626       5307952        374821 for     
4 24F                  5096083        304153             0 for     
5 24N                    10139             0             0 against 

I am hoping to eventually achieve a result that looks like this:

enter image description here

The different colored bars would be for/against, and the y-axis would be the amount spent.

Upvotes: 0

Views: 80

Answers (1)

Ben
Ben

Reputation: 30494

Before plotting, would put into long format.

library(tidyverse)
library(scales)

df %>%
  pivot_longer(cols = -c(type, fa_camp), names_to = "candidate", values_to = "amount_spent") %>%
  ggplot(aes(x = candidate, y = amount_spent, group = fa_camp, fill = fa_camp)) +
    geom_bar(stat = "identity", position = "dodge") +
    scale_y_continuous(labels = dollar)

Plot

plot with dodge bars

Upvotes: 2

Related Questions