Hi everyone, my goals for this week were to work on the error bar line graphs and descriptive statistics for my paper. My team and I met on Tuesday morning and worked on our remaining figures before the Tuesday Q&A session. We figured out a few kinks after the Q&A, but it still needed work. Emily and I went on to continue work on the error bar line graphs during the rest of the week. Here’s how I progressed:
Here is the graph I am attempting to replicate, I began working on the graph on the right first “Loneliness Changes Based on Extraversion”:
I made a few different code chunks while in the process of trying to replicate the graph, but I will just show the messy one I’ve currently been working on as it has given me the most promising results.
I first load my packages and specify my figure height and width:
library(tidyverse)
library(ggplot2)
library(dplyr)
library(reshape2)
library(extrafont)
I then made my wide data long instead to put both time 1 and time 2 under a single column:
# Lonely - Make study2 long
Study2_long <- melt(Study2,
id.vars = c("Participant_ID", "T1Extraversion"),
measure.vars = c("T1Lonely", "T2Lonely"))
I specify participant id and Extraversion columns, and a value column of T1 and T2 loneliness scores.
I then rename these columns using the colnames function:
colnames(Study2_long) = c("ID", "T1Extraversion", "Time", "Loneliness")
My dataset now looks like this:
What comes next is a bit messy… I tried many different functions, but I didn’t wanna get rid of some of them so I commented them out in case I wanted to come back to them.
# New data sets
T1_2IntroLonely <- Study2_long %>% # Trying to filter out introvert only line.
# mutate(T1IE = case_when(T1Extraversion <= 3.333333 ~ 'Introvert', #25 quartile
# T1Extraversion >= 4.416667 ~ 'Extravert')) #75 quartile
filter(T1Extraversion <= 3.33333) %>%
group_by(Time) %>%
summarise(
mean = mean(Loneliness)
)
T1_2ExtraLonely <- Study2_long %>% # Trying to filter out extravert only line.
filter(T1Extraversion >= 4.416667) %>%
group_by(Time) %>%
summarise(
mean = mean(Loneliness)
)
In the top line I try to create a new data set using that filters the most introverted people and groups by time 1 and time 2 measurements of loneliness, (as you can see, I tried to use the mutate function, but it didn’t work). I then use summarise to make a mean column that calculates mean loneliness for time 1 and 2.
I do the same beneath that with T1_2ExtraLonely, but instead it filters the most extraverted people.
Next, I tried to plot it. There was a lot of other code I left out because it looked messy and wasn’t working.
# Graphing
lonelyline = ggplot() +
labs(y = "Mean Loneliness") +
scale_x_discrete(labels = c("Before Pandemic", "After 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 = "dashed") +
geom_line(data = T1_2ExtraLonely, aes(x = Time, y = mean, group = 2), linetype = "solid") +
expand_limits(y = c(1, 3)) +
theme_classic() +
theme(text = element_text(family = "Times New Roman"),
plot.title = element_text(face = "bold", hjust = 0.5),
axis.ticks = element_line(colour = "black", size = 0.75))
lonelyline
You’ve got your basic axis labelling with the labs and scale_x_discrete functions. Then I plot the 2 data sets I made before using geom_point and geom_line, identifying them by group 1 and group 2. The linetype function was something I learned changed the look of the line in geom_line to look dashed, etc. Then, I set the limits (with needs some tweaking to look exactly the same), and the theme for the paper figures. With all those chunks together, I produced this:
My goals for week 7 are to keep working to finish off the graph, this includes:
Adding error bars
Fixing y axis scaling
Adding a legend
Merging the social connectedness graph
My other goals are to work on the week 8 presentation and to help my team if they need support.