'Grouped barchart for multiple dummy variables

I am trying to create a bar chart that shows the mean damage corresponding to 0 and 1 values for multiple dummy variables, but I really have no clue where to start.

Here the head of my data

Maybe this illustration helps to understand



Solution 1:[1]

You can use the following code:

library(ggplot2)
ggplot(data = subset(stack(df), !is.na(values)), aes(x = ind, fill = as.factor(values))) +
  geom_bar(position = "dodge", na.rm = T) +
  labs(fill = "values")

Output:

enter image description here

Data

df <- data.frame(d1 = c(0,0,0,0,0,0),
                 d2 = c(1,1,1,0,0,0),
                 d3 = c(1,0,1,0,0,1),
                 d4 = c(0,0,1,0,1,1),
                 d5 = c(NA, 0,1,0,1,NA),
                 d6 = c(0,1,1,0,1,NA),
                 d7 = c(1,1,1,NA,0,0),
                 d8 = c(1,1,1,0,1,0))  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Quinten