Hi all, apologies for the late learning log, I’ve had a full on week. For week 7 my main goal was to finish off my line error bar graph. This meant creating and merging the second graph for study 1, adding the error bars and adding a legend. My other goal was the finish and record my team presentation for the week 8 tute.
Creating the study 1 graph was essentially the same process as creating the study 2 graph. The only changes that had to be made was changing the variable names to be unique. We were confused as to why the points were not correct on the graph as we believed we had done them correctly. But when both variable names are the same in each function, the variable gets overridden with the most recent arguments. We eventually picked this up assigned each variable with a prefix that identified which study it belonged to: “SC” for social connectedness and “Lon” for the loneliness graph.
Our group spent a lot of time working on the best way to implement the error bars for our graphs. At some point I tried a whole new method of creating our graphs using the summarySEwithin function, as part of the Rmisc package:
# Mutate new function
Study2_long_meaned <- Study2_long %>%
mutate(T1IE = case_when(T1Extraversion <= 3.333333 ~ 'Introvert', #25 quartile
T1Extraversion >= 4.416667 ~ 'Extravert')) %>% #75 quartile
drop_na(T1IE) # Remove all scores from data frame that are not 'Introvert' or 'extravert' quartiles
#Condensing using summarySEwithin to create mean, SD and ci
c_long_meaned <- summarySEwithin(Study2_long_meaned, withinvars= c("Time", "T1IE"),
measurevar= "Loneliness", idvar="ID",
na.rm=FALSE, conf.interval=.95)
However, this new method, for whatever reason, gave us incorrect means. So we scrapped it:
Our next idea was the calculate the confidence intervals separately for each point and then code them into variables that we call in the geom_errorbar functions. Emily calculated each CI and I implemented them into out graphing code:
# Creating most intro and extrovert data sets by time
T1_1IntroSC <- Study1_long %>%
mutate(T1IE = case_when(EXTRAVERSION <= 3.41667 ~ 'Introvert',
EXTRAVERSION >= 4.8333 ~ 'Extravert')) %>%
filter(EXTRAVERSION <= 3.41667) %>%
group_by(Time) %>%
summarise(
mean = mean(SocialConnectedness)
)
SCupperIn = c(3.573387, 3.469464)
SClowerIn = c(3.3195757, 3.232880)
#filter extravert only for T1
T1_1ExtraSC <- Study1_long %>%
filter(EXTRAVERSION >= 4.8333) %>%
group_by(Time) %>%
summarise(
mean = mean(SocialConnectedness)
)
SCupperEx = c(4.8294, 4.589643)
SClowerEx = c(4.579, 4.301045)
################################################
T1_2IntroLonely <- Study2_long %>% #Introvert only
filter(T1Extraversion <= 3.33333) %>%
group_by(Time) %>%
summarise(mean = mean(Loneliness))
LonlowerIn = c(2.391, 2.175)
LonupperIn = c(2.671, 2.450)
T1_2ExtraLonely <- Study2_long %>% # Extravert only
filter(T1Extraversion >= 4.416667) %>%
group_by(Time) %>%
summarise(mean = mean(Loneliness))
LonlowerEx = c(1.5203, 1.568)
LonupperEx = c(1.734, 1.7788)
When merging, I used the patchwork package. The resulting graph looked like this:
socialline = ggplot() +
labs(y = "Mean Social Connectedness") +
scale_x_discrete(labels = c("Before Pandemic", "During Pandemic")) +
geom_point(data = T1_1IntroSC, aes(x = Time, y = mean, group = 1)) +
geom_point(data = T1_1ExtraSC, aes(x = Time, y = mean, group = 2)) +
geom_line(data = T1_1IntroSC, aes(x = Time, y = mean, group = 1), linetype = "dashed") +
geom_line(data = T1_1ExtraSC, aes(x = Time, y = mean, group = 2), linetype = "solid") +
geom_errorbar(aes(x = T1_1IntroSC$Time, ymin = SClowerIn, ymax = SCupperIn, width = 0.1, group = 1)) +
geom_errorbar(aes(x = T1_1ExtraSC$Time, ymin = SClowerEx, ymax = SCupperEx, width = 0.1, group = 2)) +
expand_limits(y = c(3, 5)) +
theme_classic() +
theme(text = element_text(family = "Times New Roman"),
plot.title = element_text(hjust = 0.5),
axis.ticks = element_line(colour = "black", size = 0.75)) +
ggtitle("Social Connectedness Changes \nBased on Extraversion")
lonelyline = ggplot() +
labs(y = "Mean Loneliness") +
scale_x_discrete(labels = c("Before Pandemic", "During Pandemic")) +
geom_point(data = T1_2IntroLonely, aes(x = Time, y = mean, group = 1)) +
geom_point(data = T1_2ExtraLonely, aes(x = Time, y = mean, group = 2)) +
geom_line(data = T1_2IntroLonely, aes(x = Time, y = mean, group = 1, linetype = "solid")) +
geom_line(data = T1_2ExtraLonely, aes(x = Time, y = mean, group = 2, linetype = "dashed")) +
geom_errorbar(aes(x = T1_2IntroLonely$Time, ymin = LonlowerIn, ymax = LonupperIn, width = 0.1, group = 1)) +
geom_errorbar(aes(x = T1_2ExtraLonely$Time, ymin = LonlowerEx, ymax = LonupperEx, width = 0.1, group = 2)) +
expand_limits(y = c(1, 3)) +
scale_linetype_discrete(labels = c("Most Extraverted", "Most Introverted")) +
theme_classic() +
theme(text = element_text(family = "Times New Roman"),
plot.title = element_text(hjust = 0.5),
axis.ticks = element_line(colour = "black", size = 0.75),
legend.position = "right",
legend.title = element_blank()) +
ggtitle("Loneliness Changes \nBased on Extraversion")
socialline + lonelyline
Creating the error bars was certainly a long and challenging process, but we ended up with a product in the end. While they don’t look exactly like the error bars in the original study, I’d love to learn a more efficient way of producing them.
My goal for week 8 is to present our group 5 presentation and do some more coding activities in my spare time (possibly exploring the Rmisc package).