This worksheet provides a step-by-step guide for importing and analyzing a simple dataset in R Studio, focusing on comparing pre and post-intervention scores, as well as group comparisons between CBT and PA therapy types.
For a visual demonstration of the concepts, please refer to this video: https://youtu.be/48izKsCX2so
Below is the sample dataset that will be used for the analysis.
| ClientID | group | pre_score | post_score |
|---|---|---|---|
| 1 | CBT | 15 | 10 |
| 2 | CBT | 13 | 9 |
| 3 | CBT | 17 | 11 |
| 4 | CBT | 12 | 8 |
| 5 | CBT | 16 | 12 |
| 6 | PA | 14 | 7 |
| 7 | PA | 18 | 10 |
| 8 | PA | 12 | 6 |
| 9 | PA | 15 | 8 |
| 10 | PA | 17 | 9 |
It's good practice to rename your imported dataset for easier reference.
data <- client_data
The `ggplot2` package is used for creating high-quality data visualizations.
library(ggplot2)
Calculate the average depressive symptoms scores before and after therapy.
pre_mean <- mean(data$pre_score)
pre_mean
post_mean <- mean(data$post_score)
post_mean
Organize the calculated means into a data frame suitable for plotting.
mean_data <- data.frame(
Depression = c("Pre-Therapy", "Post-Therapy"),
Mean = c(pre_mean, post_mean)
)
mean_data
A straightforward bar chart comparing the mean pre and post-therapy scores.
ggplot(mean_data, aes(x = Depression, y = Mean, fill = Depression)) +
geom_bar(stat = "identity") +
labs(title = "Comparison of Pre and Post Therapy Depressive Symptoms",
x = "Assessment Time",
y = "Mean Depressive Symptoms Score")
An enhanced bar chart with data labels, custom colors, and a minimalist theme.
ggplot(mean_data, aes(x = Depression, y = Mean, fill = Depression)) +
geom_bar(stat = "identity", show.legend = FALSE, width = 0.6) +
geom_text(aes(label = round(Mean, 1)), vjust = -0.5, size = 5) +
labs(title = "Comparison of Pre and Post Therapy Depressive Symptoms",
x = "Assessment Time",
y = "Mean Depressive Symptoms Score") +
theme_minimal() +
scale_fill_manual(values = c("skyblue", "lightcoral")) +
ylim(0, 16)
Calculate the average post-therapy scores for each group (CBT and PA).
cbt_post_mean <- mean(data$post_score[data$group == "CBT"])
cbt_post_mean
pa_post_mean <- mean(data$post_score[data$group == "PA"])
pa_post_mean
Organize the group means into a data frame for plotting.
group_mean_data <- data.frame(
Group = c("CBT", "PA"),
Mean_Post_Score = c(cbt_post_mean, pa_post_mean)
)
A basic bar chart comparing the mean post-therapy scores between the CBT and PA groups.
ggplot(group_mean_data, aes(x = Group, y = Mean_Post_Score, fill = Group)) +
geom_bar(stat = "identity") +
labs(title = "Comparison of Post-Therapy Scores by Group",
x = "Therapy Group",
y = "Mean Post-Therapy Score")
An enhanced bar chart for group comparison with data labels, custom colors, and a clean theme.
ggplot(group_mean_data, aes(x = Group, y = Mean_Post_Score, fill = Group)) +
geom_bar(stat = "identity", show.legend = FALSE, width = 0.6) +
geom_text(aes(label = round(Mean_Post_Score, 1)), vjust = -0.5, size = 5) +
labs(title = "Comparison of Post-Therapy Scores by Group",
x = "Therapy Group",
y = "Mean Post-Therapy Score") +
theme_minimal() +
scale_fill_manual(values = c("orange", "purple")) +
ylim(0, 12)