R Studio Data Analysis Worksheet

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.

Demonstration Video

For a visual demonstration of the concepts, please refer to this video: https://youtu.be/48izKsCX2so

Sample Data

Below is the sample dataset that will be used for the analysis.

ClientID group pre_score post_score
1CBT1510
2CBT139
3CBT1711
4CBT128
5CBT1612
6PA147
7PA1810
8PA126
9PA158
10PA179

R Studio Setup & Data Import

RStudio Cloud (Posit.cloud)

RStudio Desktop

Rename Data

It's good practice to rename your imported dataset for easier reference.

data <- client_data

Load ggplot2 Package

The `ggplot2` package is used for creating high-quality data visualizations.

library(ggplot2)

Calculate Mean Scores (Pre and Post)

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

Create Data Frame for Means

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

Bargraph for Before and After Therapy Scores

Option 1: Basic Bargraph

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")

Option 2: Advanced Bargraph

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)

Group Comparison: CBT vs. PA

Calculate Mean Post Scores by Group

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

Create Data Frame for Group Means

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)
)

Bar Chart for Two Groups

Option 1: Basic Group Bargraph

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")

Option 2: Advanced Group Bargraph

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)