-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path99990-ggplot2_2way_SE.R
55 lines (47 loc) · 2.48 KB
/
99990-ggplot2_2way_SE.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
## this script is meant to be sourced from other scripts ##
###########################################
####### Run code but do not modify ########
###########################################
(dat1 = describeBy(dat$dv, list(dat$iv1, dat$iv2), mat = TRUE, digits = 2))
names(dat1)[names(dat1) == 'group1'] = 'iv1'
names(dat1)[names(dat1) == 'group2'] = 'iv2'
dat1$se = dat1$sd/sqrt(dat1$n) # calculates SE for error bars if desired
### tells R to provide a plot in APA style, figure is MS ready
limits = aes(ymax = mean + (1.96*se), ymin=mean - (1.96*se))
dodge = position_dodge(width=0.9)
apatheme=theme_bw() +
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.border=element_blank(),
axis.line=element_line(),
text=element_text(family='Times'))
### now the actual figure
figure_1 <- dat1 %>%
mutate(iv1 = fct_reorder(iv1, desc(iv1)),
iv2 = fct_reorder(iv2, desc(iv2))) %>%
ggplot(aes(x = iv1, y = mean, group = iv2)) +
geom_bar(stat = "identity", position = "dodge", aes(fill = iv2)) +
geom_errorbar(limits, position=dodge, width=0.25) +
coord_cartesian(ylim=c(y_axis_low, y_axis_high)) + # this is the range of the y-axis
scale_y_continuous(breaks=seq(y_axis_low, y_axis_high, y_increment)) + # this is the increment ticks on the y-axis
ylab(y_label) +
scale_x_discrete(x_label, labels = x_values) +
guides(fill=guide_legend(title = mod_label)) +
apatheme +
theme(legend.position = legend_loc) +
scale_fill_grey()
### now the actual figure
figure_2 <- dat1 %>%
mutate(iv1 = fct_reorder(iv1, desc(iv1)),
iv2 = fct_reorder(iv2, desc(iv2))) %>%
ggplot(aes(x = iv2, y = mean, group = iv1)) +
geom_bar(stat = "identity", position = "dodge", aes(fill = iv1)) +
geom_errorbar(limits, position=dodge, width=0.25) +
coord_cartesian(ylim=c(y_axis_low, y_axis_high)) + # this is the range of the y-axis
scale_y_continuous(breaks=seq(y_axis_low, y_axis_high, y_increment)) + # this is the increment ticks on the y-axis
ylab(y_label) +
scale_x_discrete(mod_label, labels = mod_values) +
guides(fill=guide_legend(title = x_label)) +
apatheme +
theme(legend.position = legend_loc) +
scale_fill_grey()