What were the goals for this week?
The goal this week was to finish off figure 1 with Angelina and to get started on our presentation. We needed to first rubber duck all our code and then decide on what we would present.
How did I achieve these goals?
Figure 1 was still giving us some difficulties after figuring out what was wrong with the geom_col() scaling last week. The first thing I wanted to figure out was how to get the geoms to not stack in a prettier way. Previously, I had just used added .3 to the cond variable when plotting the column graphs in order for them to be shifted upwards. JennyS showed me a better way to do this by using position_nudge().
library(tidyverse)
library(janitor) #used to read csv
library(ggplot2) #used to plot column
library(ggdist) #used for raincloud plot
library(gridExtra) #combines both plots
data1 <- read_csv("~/R files/git/group4_nicols/data/Nichols_et_al_data.csv")
data1 <- data1 %>%
filter(include == 0) %>%
rename(cond = con,
claimpercent = claim,
claimmoney = moneyclaim,
CT_practice = `completion time (practice included)`,
CT_payments = `completion time (payments only)`,
religiosity = relig,
religion = Religion) %>%
select(site, claimpercent, id, cond) %>%
mutate(claimpercent = claimpercent * 100) %>%
as_tibble()
# data by condition
data1$cond[data1$cond==4] <- 0 # Make religious prime the reference category
data1$cond[data1$cond==1] <- 4 # This is in a weird order as R reads the code line by line, so if we go from top to bottom,
data1$cond[data1$cond==3] <- 1 # we're changing the number twice which screws up our dataframe
data1$cond[data1$cond==4] <- 3
# data by site
data1$site[data1$site==1] <- 0
data1$site[data1$site==3] <- 1
# makes USA = 0, Japan = 1, Czech Republic = 2
data1 <- data1 %>%
mutate(numberOf = (cond == 0) * 100 + (cond == 1 | cond == 2) * 103 + (cond == 3) * 102) %>%
mutate(claimpercent2 = claimpercent / numberOf)
# data by site
data1 <- data1 %>%
mutate(numberOf2 = (site == 0) * 123 + (site == 2) * 127 + (site == 1) * 156) %>%
mutate(claimpercent3 = claimpercent / numberOf2)
# data by condition
group_cond <- data1 %>%
group_by(cond) %>%
summarise(mean = mean(claimpercent, na.rm = TRUE),
sd = sd(claimpercent, na.rm = TRUE),
n = n()) %>%
drop_na() %>%
mutate(se = sd / sqrt(n),
lowerCI = mean - qt(1 - (0.05/2), n - 1) * se,
upperCI = mean + qt(1 - (0.05/2), n - 1) * se) %>%
ungroup()
# data by site
group_site <- data1 %>%
group_by(site) %>%
summarise(mean = mean(claimpercent, na.rm = TRUE),
sd = sd(claimpercent, na.rm = TRUE),
n = n()) %>%
drop_na() %>%
mutate(se = sd / sqrt(n),
lowerCI = mean - qt(1 - (0.05/2), n - 1) * se,
upperCI = mean + qt(1 - (0.05/2), n - 1) * se) %>%
ungroup()
fig1_cond <- ggplot(data1, aes(x = cond)) +
geom_col(
aes(y = claimpercent2),
width = .3
) +
ggdist::stat_halfeye(
aes(y = claimpercent),
adjust = .5,
width = .4,
.width = 0,
point_colour = NA,
position = position_nudge(x = 0.17, y = 0)
) +
geom_errorbar(data = group_cond,
aes(ymin = lowerCI, ymax = upperCI),
width = .3) +
coord_flip() +
expand_limits(x = 0, y = 0) +
scale_x_continuous(name = NULL,
expand = c(0,0),
breaks = c(0, 1, 2, 3),
labels = c("Control","Noise","Secular","Religious")
) +
scale_y_continuous(name = "Percent Claimed",
expand = c(0,0),
breaks = c(0, 20, 40, 60, 80, 100)
) +
ggtitle(
label = "Data by condition"
) +
theme_classic() +
ggtitle(label = "A.") +
facet_grid(. ~ "Data by condition")
plot(fig1_cond)Angelina also finished off the second plot in the figure.
fig1_site <- ggplot(data1, aes(x = site)) +
geom_col(
aes(y = claimpercent3),
width = .3
) +
geom_errorbar(data = group_site,
aes(ymin = lowerCI, ymax = upperCI),
width = .3
) +
ggdist::stat_halfeye(
aes(y = claimpercent),
adjust = .5,
width = .3,
.width = 0,
point_colour = NA,
position = position_nudge(x = 0.2, y = 0)
) +
coord_flip() +
expand_limits(x = 0, y = 0) +
scale_x_continuous(name = NULL,
expand = c(0,0),
breaks = c(0, 1, 2),
labels = c("USA","Japan","Czech Rep.")
) +
scale_y_continuous(name = "Percent Claimed",
expand = c(0,0),
breaks = c(0, 20, 40, 60, 80, 100)
) +
theme_classic() +
ggtitle(
label = "B.") +
facet_grid(. ~ "Data by site")
plot(fig1_site)We’re now only stuck on one part. We are unsure of how to colour the plots according to the paper. Using scale_fill_manual() does not work as our scale uses continuous values. However, using discrete values changes the position of the geoms for some reason. Once we figure this out, we are done with all our figures!
What are the goals for next week?
The goals for next week are to get our presentation ready for the class! It has been a long journey to get to where we are but I am massively happy with our team’s efforts in replicating the figures from the paper.