knitr::opts_chunk$set(echo = TRUE)
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)")
ggplot(data, aes(x = Glucose, y = Cholesterol, col = Diagnosis)) +
geom_point(size = 2) +
labs(
title = "Glucose vs Cholesterol",
x = "Glucose",
y = "Cholesterol",
color = "Diagnosis"
)
ggplot(data, aes(x = BMI)) +
geom_histogram(bins = 30) +
labs(
title = "Distribution of BMI in the Study Population",
x = "BMI",
y = "Count"
)
ggplot(data, aes(x = Cholesterol, col = Gender)) +
geom_density() +
labs(
title = "Cholesterol Distribution by Gender",
x = "Cholesterol",
y = "Density",
)
ggplot(data, aes(x = Diagnosis, y = Glucose, fill = Diagnosis)) +
geom_boxplot() +
labs(
title = "Glucose Levels Across Diagnostic Groups",
x = "Diagnosis",
y = "Glucose"
)
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"
)
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)"
)
ggplot(data, aes(x = Diagnosis)) +
geom_bar(fill = "skyblue") +
labs(
title = "Number of Patients by Diagnosis",
x = "Diagnosis",
y = "Number of Patients"
)
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"
)
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()
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()
# 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()
#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)
)
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)
)