The goal:
Preliminaries
load packages
using the same packages as usual:
library(tidyverse) #for data wrangling and visualisation
library(gt) #for creating a table
library(janitor) #for cleaning names and other possibly handy functions
library(plotrix) #for calculating standard error of the mean
read in the data
Again I am reading in the clean data file I saved from when creating table 1. Just to recap, this file excludes all data from excluded participants. There are more variables than I need to reproduce plot 2, so I will need to do some data wrangling.
cleandata <- read_csv("cleandata.csv")
Data wrangling
obtaining relevent variables
Because this plot looks like it builds off the values from table 3, I am going to see if I can use the dataframe i created for table 3 called biaslevelsbycondition to mutate new values to find the immediate and week changes in bias levels for both cued and uncued conditions.
print(biaslevelsbycondition)
I just realised that the dataframe above includes means and sds but I don’t want that so I will just use the same first part of code for that variable with the exception of base variables since we won’t be needing that for this graph, omit the summarise part and replace it with mutate instead to create new variables for the graph.
Before I use mutate I’m using print to remind myself of the variables and values. Note I ordered the variables such that using the when mutate function would be easier to see what variable I’m subtracting from another.
biaslevelsbycondition <- cleandata %>%
select(postIATcued, preIATcued, postIATuncued, preIATuncued, weekIATcued, weekIATuncued)
print(biaslevelsbycondition)
*creating new variables**
Now onto using mutate to new variables to be used for my graph. After I’ve created my new variables, I’m using select to choose these new variables only for my graph.
biaschange <- biaslevelsbycondition %>%
mutate(immed_cued = postIATcued - preIATcued,
immed_uncued = postIATuncued - preIATuncued,
week_cued = weekIATcued - preIATcued,
week_uncued = weekIATuncued - preIATuncued) %>%
select(immed_cued, immed_uncued, week_cued, week_uncued)
print(biaschange)
obtaining summary statistics, mean and standard error
great now I can figure out the means and standard error of each variable: again I’m using the summarise and across function to do so. In this chunk of code, each variable I want to find the means and standard errors for have the letters ‘cued’ in the so I’m inserting that into the contains function so I don’t have to type out all the variables in full.
I’m using a new function here called std.error from a new package (plotrix) which calculates sstandard error directly without having me to type the whole formula for it. I’m going to try it out and add it in the list function to try and obtain mean and std error in one dataframe.
#biaschangesummary <- biaslevelchange %>%
summarise(across(contains("cued"), list(mean = mean, stderror = std.error(biaschange))))
Hmmm okay, I’m getting an error here, perhaps we can’t find standard error within the summarise function since the std.error function comes from a different package (plotrix) from that of summarise (dplyr). When I run std.error by itself outside of the chunk above, it works to find the standard error from every variable, so what if I just pipe the std.error function after usisng summaries to find the mean
#biaschangesummary <- biaslevelchange %>%
summarise(across(contains("cued"), list(mean = mean))) %>%
std.error(biaschange)
#print(biaschangesummary)
Alright I guess that didn’t work. Perhaps I cannot obtain the mean and sstandard error values in one dataframe. So I’m just going to resort back to the old method, and find the mean and standard error in seperate data frames:
# THIS THE SUCCESSFUL CHUNK OF CODE FOR MEANS AND STD ERRORS
biaschangemean <- biaschange %>%
summarise(across(contains("cued"), list(mean = mean))) #finding the mean of each variable
print(biaschangemean)
biaschangeerror <- std.error(biaschange) #finding the standard error of each variable
print(biaschangeerror)
Recreating plot 2
creating the table
plot2 <- tibble(
time =
) #i didnt finish this coz i had the big brain idea i wanted to try
WAIT I HAVE BIG BRAIN I’M GOING TO TRY SOMETHING DIFFERENT USING PIVOT LONGER
testpivot <- originaldata %>%
filter(exclude == "no") %>%
select(ParticipantID, postIATcued, preIATcued, postIATuncued, preIATuncued, weekIATcued, weekIATuncued) %>%
mutate(immed_cued = postIATcued - preIATcued,
immed_uncued = postIATuncued - preIATuncued,
week_cued = weekIATcued - preIATcued,
week_uncued = weekIATuncued - preIATuncued) %>%
select(ParticipantID, immed_cued, immed_uncued, week_cued, week_uncued) %>%
pivot_longer(testpivot, cols = c(immed_cued, immed_uncued, week_cued, week_uncued), names_to = "time1", values_to = "bias_change")
testpivot_immedcued <- testpivot %>%
filter(time1 == "immed_cued")
testpivot_immeduncued <- testpivot %>%
filter(time1 == "immed_uncued")
testpivot_weekcued <- testpivot %>%
filter(time1 == "week_uncued")
testpivot_weekuncued <- testpivot %>%
filter(time1 == "week_uncued")
testarranged <- bind_rows(testpivot_immedcued, testpivot_immeduncued, testpivot_weekcued, testpivot_weekuncued) %>%
mutate(timeframe = )
#OKAY TRY STH ELSE
testpivot2 <- testpivot %>%
mutate(time2 = case_when(time1 == immed_cued ~ "1", time1 == immed_uncued ~ "2", time1 == week_cued ~ "3", time1 == week_uncued ~ "4"))
#why doesn't this not work
testpivot2 <- testpivot %>%
mutate(time2 = recode(time1, `immed_cued` = "1", `immed_uncued` = "2", `week_cued` = "3", `week_uncued` = "4")) %>%
mutate(time3 = recode(time1, `immed_cued` = "1", `immed_uncued` = "3", `week_cued` = "2", `week_uncued` = "4"))
withtimeandcue <- testpivot2 %>%
mutate(timeframe = ifelse(time2 > 2, "week", "immediate")) %>%
mutate(cuecondition = ifelse(time3 > 2, "uncued", "cued"))
okay ggplot time pls work
ggplot(withtimeandcue, aes(x = timeframe,y = bias_change, fill = cuecondition))+
geom_col(stat = "identity", position = "dodge")
#this one workss!!
ggplot(withtimeandcue, aes(x = factor(timeframe), y = bias_change, fill = cuecondition))+ stat_summary(fun = "mean", geom = "bar", position = "dodge")
geom_errorbar()
the graph above doesn’t work with error bars. a very sad moment. Perhaps I have to revise the old method of graphing from a tibble
time1 <- c(rep("immediate",2),rep("week",2))
Condition <-rep(c("cued","uncued"),2)
bias_change <- c(0.09593775, -0.05390545, 0.1890689, 0.09643346)
stderror <- c(0.09759788, 0.10297893, 0.11593440, 0.09008655)
data1 <- data.frame(time1, condition, bias_change, stderror)
head(data1)
fig4 <- ggplot(data = data1, aes(x = time1, y = bias_change,fill = condition)) +
geom_bar(position = "dodge", stat = "identity", alpha=0.7) +
geom_errorbar(aes(x= time1, ymin=bias_change-stderror, ymax=bias_change+stderror), width=0.4, colour="grey", alpha= 0.9, position = position_dodge(0.9)) +
ylim(-0.2, 0.4) +
labs(x = "", y = "Bias Change", caption = "Fig 4. Change in implicit bias levels at the immediate and one-week delay tests.")
print(fig4)