Alyssa C
Alyssa C

Reputation: 51

Graphing binary column sums in R, and then overlay those columns with a ratio of categorical variables

I am trying to graph qualitative policy analysis data in R. My database has one row for each policy, and then columns for binary variables, conditions that are coded "1" if that condition is met. Finally, each row also contains a column for whether that policy is mandatory, voluntary, or partial.

I want to create a bar chart that sums the columns, then colors in the bars according to what percentage of the sum is Mandatory, Voluntary, or Partial.

The ideal outcome would be to create a bar chart like the one below, but coded by color according to the ratio of Mandatory, Voluntary, or Partial policies

Policy Bar chart

Here is some sample data in the same format:

df<- data.frame(ID=c(1,2,3,4,5,6),
            policy=c("Policy A", "Policy B", "Policy C", "Policy D", 
            "Policy E","Policy F" ),
            Data_collection= c(1, 0, 0, 1, 1, 0),
            Handling_release= c(0, 1, 0, 1, 0, 1),
            Gear_modification= c(1, 0, 0, 1, 1, 0),
            Stength=c("M", "V", "M", "P", "P", "M"),
            stringsAsFactors=FALSE)

Upvotes: 1

Views: 405

Answers (1)

MrFlick
MrFlick

Reputation: 206496

It looks like you really need to reshape your data to a proper tidy format to make plotting easier. For example you could do

library(dplyr)
library(tidyr)
library(ggplot2)

df %>% 
  pivot_longer(Data_collection:Gear_modification) %>% 
  filter(value==1) %>% 
  ggplot(aes(name, fill=Stength)) +
  geom_bar()

For the sample data provided this gives

stacked bar plot

For adding the total on top see this existing question: draw the sum value above the stacked bar in ggplot2

Upvotes: 2

Related Questions