Skip to content

Instantly share code, notes, and snippets.

@RobertTalbert
Created March 26, 2024 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertTalbert/7909228698cc767729417fb1f87e947e to your computer and use it in GitHub Desktop.
Save RobertTalbert/7909228698cc767729417fb1f87e947e to your computer and use it in GitHub Desktop.
# Libraries
library(dplyr)
library(tidyverse)
library(ggplot2)
# Load data
colnames <- c("time", "email", "name", "section",
"challenge", "support",
"competence", "autonomy", "relatedness",
"elaborate", "needs")
## Change name of input file as needed
responses <- read_csv("put the name of your CSV here",
skip = 1,
col_names = colnames)
# Summary stats
summary(responses)
# Combined boxplots
## Challenge and support variables
y_range <- range(c(responses$challenge, responses$support))
par(mfrow = c(1,2))
boxplot(responses$challenge, main = "Challenge",
col = "lightblue", border = "black", ylim = y_range)
boxplot(responses$support, main = "Support",
col = "lightgreen", border = "black", ylim = y_range)
par(mfrow = c(1,1))
## Self Determination variables
y_range <- range(c(responses$competence, responses$autonomy, responses$relatedness))
par(mfrow = c(1,3))
boxplot(responses$competence, main = "Competence",
col = "lightblue", border = "black", ylim = y_range)
boxplot(responses$autonomy, main = "Autonomy",
col = "lightgreen", border = "black", ylim = y_range)
boxplot(responses$relatedness, main = "Relatedness",
col = "lightcoral", border = "black", ylim = y_range)
par(mfrow = c(1,1))
# The "bubble plot"
## Start with a basic scatterplot of support vs challenge
## Condense responses to a single data frame with unique rows plus frequencies
chall_support <- responses[, c(5,6)] |>
group_by_all() |>
summarise(freq = n())
## Scatterplot of responses with frequencies labelled
chall_support |> ggplot(aes(challenge, support)) +
geom_hline(yintercept = 3, linetype = "dashed", color="black", size=0.2) +
geom_vline(xintercept = 3, linetype = "dashed", color="black", size=0.2) +
geom_point(color = "lightblue", size = chall_support$freq, alpha = 1) +
geom_text(aes(label = sprintf("%d", freq)), size = 5) +
labs(title = "Support vs Challenge", x = "Challenge", y = "Support") +
scale_x_continuous(limits = c(1,5)) +
scale_y_continuous(limits = c(1,5)) +
coord_cartesian(clip = "off") +
theme(plot.margin = margin(1,1,1,1,"cm"))
## Trying it with SDT variables instead
## Change the columns as needed
## 7 = competence
## 8 = autonomy
## 9 = relatedness
sdt_paired <- responses[, c(8,9)] |>
group_by_all() |>
summarise(freq = n())
## Change variable names as needed
sdt_paired |> ggplot(aes(autonomy, relatedness)) +
geom_hline(yintercept = 3, linetype = "dashed", color="black", size=0.2) +
geom_vline(xintercept = 3, linetype = "dashed", color="black", size=0.2) +
geom_point(color = "lightblue", size = auto_related$freq, alpha = 1) +
geom_text(aes(label = sprintf("%d", freq)), size = 5) +
labs(title = "Autonomy vs Relatedness", x = "Autonomy", y = "Relatedness") +
scale_x_continuous(limits = c(1,5)) +
scale_y_continuous(limits = c(1,5)) +
coord_cartesian(clip = "off") +
theme(plot.margin = margin(1,1,1,1,"cm"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment