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