knitr::opts_chunk$set(echo = TRUE)

1 Section A – Exploring Clinical and Metabolic Data

1.1 Problem 1: Exploring Blood Pressure and BMI

library(ggplot2)

setwd("C:/Users/ahuque/Documents/R_Oxfordbio")
data <- read.csv("biomed_visualization_data.csv")

ggplot(data, aes(x = BMI, y = BP_Sys,  col = Gender)) +
  geom_point() +
  labs(title = "Exploring Blood Pressure and BMI",
     x = "Body Mass Index (BMI)", 
     y = "Systolic Blood Pressure (SBP)")

1.2 Problem 2: Investigating Glucose vs Cholesterol

ggplot(data, aes(x = Glucose, y = Cholesterol, col = Diagnosis)) +
  geom_point(size = 2) +
  
  labs(
    title = "Glucose vs Cholesterol",
    x = "Glucose",
    y = "Cholesterol",
    color = "Diagnosis"
  )

1.3 Problem 3: Distribution of BMI

ggplot(data, aes(x = BMI)) +
  geom_histogram(bins = 30) +
  labs(
    title = "Distribution of BMI in the Study Population",
    x = "BMI",
    y = "Count"
  ) 

1.5 Problem 5: Comparing Glucose Levels Across Diagnostic

ggplot(data, aes(x = Diagnosis, y = Glucose, fill = Diagnosis)) +
  geom_boxplot() +
  labs(
    title = "Glucose Levels Across Diagnostic Groups",
    x = "Diagnosis",
    y = "Glucose"
  )

2 Section B – Combining Layers and Adding Complexity

2.1 Problem 6: Enhanced Glucose Visualization with Jitter

ggplot(data, aes(x = Diagnosis, y = Glucose, fill = Diagnosis, col = Diagnosis)) +
  geom_boxplot(alpha = 0.7) +
  geom_jitter(width  = 0.2) +
  labs(
    title = "Glucose Levels Across Diagnostic Groups",
    x = "Diagnosis",
    y = "Glucose"
  )

2.2 Problem 7: Visualizing Blood Pressure Components

library(tidyr)

bp_long <- data |>
  pivot_longer(
    cols = c(BP_Sys, BP_Dia), # I want only two columns, that is why selected with C()
    names_to = "BP_Type",
    values_to = "Value"
  )

View(bp_long)

ggplot(bp_long, aes(x = BP_Type, y = Value, fill = Gender, col = Gender)) +
  geom_violin(alpha = 0.7) +
  #geom_jitter(width  = 0.2) + it gave me weird jitter points
  labs(
    title = "Blood Pressure by Gender",
    x = "Blood Pressure Type",
    y = "Blood Pressure (mmHg)"
  )

2.3 Problem 8: Count of Patients by Diagnosis

ggplot(data, aes(x = Diagnosis)) +
  geom_bar(fill = "skyblue") +
  labs(
    title = "Number of Patients by Diagnosis",
    x = "Diagnosis",
    y = "Number of Patients"
  )

2.4 Problem 9: Faceted View by Treatment Type

ggplot(data, aes(x = Cholesterol, y = Glucose, col = Diagnosis)) +
  geom_point(size = 3) +
  facet_wrap(~Treatment) +
  labs(
    title = "Cholesterol vs Glucose by Treatment",
    x = "Cholesterol",
    y = "Glucose",
    color = "Diagnosis"
  )

2.5 Problem 10: Custom Color Palette for Publication

library(RColorBrewer)

ggplot(data, aes(x = Cholesterol, y = Glucose, col = Diagnosis)) +
  geom_point(size = 3) +
  facet_wrap(~Treatment) +
  scale_color_brewer(palette = "Set2") +
  labs(
    title = "Cholesterol vs Glucose by Treatment",
    x = "Cholesterol",
    y = "Glucose",
    color = "Diagnosis"
  ) +
  theme_classic()

3 Section C – Clinical Summaries and Patterns

3.1 Problem 11: Average Insulin Levels by Diagnosis

ggplot(data, aes(x = Diagnosis, y = Insulin, fill = Diagnosis, col = Diagnosis)) +
  geom_boxplot(alpha = 0.7) +
  geom_jitter(width = 0.2, alpha = 0.6) +
  labs(
    title = "Insulin Levels by Diagnosis",
    x = "Diagnosis",
    y = "Insulin"
  ) +
  theme_classic()

3.2 Problem 12: Gender Differences in Blood Pressure

# We already converted into bp_long in the problem #7, so I am using the same table again.

ggplot(bp_long, aes(x = Gender, y = Value, fill = BP_Type)) +
  geom_boxplot(alpha = 0.7) +
  labs(
    title = "Comparison of Systolic and Diastolic BP by Gender",
    x = "Gender",
    y = "Blood Pressure (mmHg)"
  ) +
  theme_classic()

3.3 Problem 13: Stage-Wise Tumor Biomarker Correlation

#This could be useful to generate GeneOntology-related graph 
#We can make the geom_point of expressed GO term according to total expressed Gene count

ggplot(data, aes(x = Glucose, y = Insulin, col = Cancer_Type)) +
  geom_point(aes(size = Stage), alpha = 0.7) +
  geom_smooth(method = "lm", se = FALSE) +
  labs(
    title = "Glucose vs Insulin by Cancer Type and Tumor Stage",
    x = "Glucose",
    y = "Insulin",
    color = "Cancer Type",
    size = "Stage"
  ) +
  theme_classic()
## `geom_smooth()` using formula = 'y ~ x'

#Section D – Medium to Hard: Integrative & Publication-Ready Visuals ## Problem 14: Exploring Cardio-metabolic Risk by Treatment

ggplot(data, aes(x = BMI, y = Glucose, col = Diagnosis)) +
  geom_point(size = 3, alpha = 0.7) +
  facet_wrap(~Treatment) +
  labs(
    title = "BMI and Glucose Across Treatment Groups",
    x = "Body Mass Index (BMI)",
    y = "Glucose",
    col = "Metabolic Status" # This is the way to change legend title
    
  ) +
  theme_classic() +
  
  theme(
    plot.title = element_text(size = 15,hjust = 0.5),
    axis.title = element_text(size = 12),
    axis.text = element_text(size = 10),
    legend.title = element_text(size = 12)
  )

3.4 Problem 15: Building a Publication-Ready Clinical Summary Figure

ggplot(data, aes(x = Diagnosis, y = Insulin, fill = Diagnosis, col = Diagnosis)) +
  geom_boxplot(alpha = 0.9) +
  geom_jitter(width = 0.3) +
  
  scale_fill_brewer(palette = "Set3") +
  scale_color_brewer(palette = "Set3") +
  
  labs(
    title = "Serum Insulin Distribution Across Diagnostic Groups",
    subtitle = "Study Population: n = 120",
    x = "Diagnosis",
    y = "Serum Insulin",
    fill = "Metabolic Status",
    col = "Metabolic Status",
    caption = "Source: Simulated Biomedical Dataset"
  ) +
  
  theme_classic() +
  
  theme(
    plot.title = element_text(size = 15, hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5),
    plot.caption = element_text(hjust = 0.5),
    axis.title.x = element_text(hjust = 0.5),
    axis.title.y = element_text(hjust = 0.5)
  )