0

I want to have two charts containing multiple horizontal bar graphs, each showing mean values of one of the two variables: fear and expectation. The bar graphs should be grouped by the dummies.

I have created single bar graphs with the mean values of fear and expectation grouped by each of the dummies but I don't know how to combine them properly.

x = data.frame(
id   = c(1, 2, 3, 4, 5),
sex = c(1, 0, 1, 0, 1),
migration  = c(0, 1, 0, 1, 0),
handicap = c(0, 1, 1, 1, 0),
east = c(0, 1, 1, 1, 0),
fear = c(1, 3, 4, 6, 3),
expectation = c(2, 3, 2, 5, 4))

I want to have it look like this basically: https://ibb.co/3fz0GQ4

Any help would be greatly appreciated.

Argon
  • 752
  • 7
  • 18
Helbisch
  • 3
  • 2
  • Have a look at [this](https://stackoverflow.com/questions/15158230/barplot-with-2-variables-side-by-side/56716215#56716215) question. It may help you with your query. – Argon Jul 16 '19 at 09:23

1 Answers1

0

TO get to the plot you show, you will need to reshape a bit your data:

library(tidyverse)

x2 <- x%>%
  gather(fear, expectation, key = "group", value = "value")%>%
  gather(sex, migration, handicap, east, key = "dummies", value = "dum_value")%>%
  group_by(group, dummies, dum_value)%>%
  summarize(prop = mean(value))

Then you can easily get to the plot:

x2%>%
  ggplot(aes(y= prop, x = dummies, fill = factor(dum_value)))+
  geom_bar(stat = "identity", position = "dodge")+
  coord_flip()+
  facet_wrap(~group)

aaumai
  • 263
  • 1
  • 7
  • @Helbisch if you're happy with the answer, it is a sign of appreciation to accept it - you can do this by checking the tick next to the answer – tjebo Jul 16 '19 at 11:25