-1

I'm trying to replicate these histograms of a study in R.

I have a huge dataset from this study so I don't think I'll be able to paste it here, but here is a short version:

menutype menuselection belieflearn learned
5              1           0          0
11             1           1          0
2              3           0          0
2              3           0          0
2              1           0          0
2              1           0          0
10             1           0          0
12             3           0          0
8              3           0          1
12             3           0          0

The idea is the following: first, I select only the variables where the variable "menuselection == 3". Then, for these variables, for each value of menutype (in range 1:7) that corresponds respectively to "GUILT", "SSB0"... on the graph, I compute the frequency depending on if the player expected option 1 (so if belieflearn == 1) and the frequency if the player chose the option 1 (so if learned == 1).

I think factor() need to be used here but I don't quite grasp how. I've followed this thread and I've tried this :

df2 <- data.frame(
  menutype =  factor(df$menutype, labels = c("GUILT", "SSB0", "SSB1", "FLEX0", "FLEX1", "STD", "FLEX01", "test1","test2", "test3", "test4", "test5" )),
  Belief = factor(df$belieflearn, labels= c("Believe not learn", "Believe Learn")),
  Choice = factor(df$learned, labels= c("Not learn", "Learn"))
)


df3 <- df2 %>%
  count(Belief, menutype) %>%
  group_by(Belief) %>% 
  mutate(prop = n / sum(n))


ggplot(data = df3, aes(menutype, prop , fill = Belief)) +  
  geom_bar(stat = "identity", position = "dodge")

Which is working but I want to exclude values for which menutype>7 (I put test1, test2 to get the factor() working but optimally, I want to get rid of them). I tried exclude() but without success.

I also didn't specify for menuselection == 3. Maybe a loop should do it?

I'm getting this as a graph. Obviously, there is something I'm doing wrong, because I should have two bars for each menutype with Belief and learned proportion.

Also, I'm fairly new to R (and to StackOverflow) so if there is something that I should add to this thread, tell me!

Thanks for the help.

EDIT: I found the Stata code used to generate the graph in the original study, so here it is:

  graph bar (mean) belieflearn learned if menuselection==3, over(menutype, relabel(1 "{it:{stSerif:GUILT}}" 2 "{it:{stSerif:SSB_{subscript:0}}}" 3 "{it:{stSerif:SSB_{subscript:1}}}" 4 "{it:{stSerif:FLEX_{subscript:0}}}" 5 "{it:{stSerif:FLEX_{subscript:1}}}" 6 "{it:{stSerif:STD_{subscript:0}}}" 7 "{it:{stSerif:FLEX_{subscript:0v1}}}" ))  ///
ytitle("fraction of subjects") yvaroptions(relabel(1 "expected Option 1 (reading)" 2 "chose Option 1 (reading)")) title("classification based on rank ordering") ///
bar(1, bcolor(navy)) bar(2, bcolor(red*0.4) lcolor(red*0.9))  ///
ylab(0(0.2)1, nogrid) blabel(bar, position(outside) format(%9.2f)) graphregion(color(white)) saving(f1, replace) nodraw
Jauhnax
  • 95
  • 1
  • 10

2 Answers2

1

If I have understood your question this may be what you are after

ggplot(data = df3, 
aes(interaction(menutype,Belief),  #get combination of groups
prop , fill = Belief) + 
geom_bar(stat = "identity", position = "dodge")+
scale_x_discrete(labels = levels(df3$menutype)) # adds clean label to x
e.matt
  • 836
  • 1
  • 5
  • 12
0

Generating some fake data:

set.seed(101)
df <- data.frame("menutype" = rep(c("GUILT", "SSB0", "SSB1", "FLEX0", "FLEX1", "STD", "FLEX01", "test1","test2", "test3", "test4", "test5"), each = 2), 
                 "value" = sample(c(2:10), 24, replace = TRUE),
                 "group" = rep(c("Not learn", "Learn"), times = 12))

Producing the plot:

library(ggplot2)
ggplot(df, aes(menutype, value))+
  geom_bar(aes(fill = group, color = group), stat = "identity", position = position_dodge(0.8), width = 0.7 )+
  scale_fill_brewer(palette = "Set1")+
  theme_classic()

The result:

enter image description here

Basically, you need 3 variables:

  1. Your value (y-axis)
  2. Your menutype (x-axis)
  3. Color variable group
Borexino
  • 802
  • 8
  • 26