#---------------------------------------- Use filter data to plot scatter Plot from long data with R and p value#install.packages("reshape2")#install.packages("ggsignif")#install.packages("ggpubr")library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.4.2
library(readr)
Warning: package 'readr' was built under R version 4.4.2
library(dplyr)
Warning: package 'dplyr' was built under R version 4.4.2
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(Hmisc) # For Spearman's rank correlation
Warning: package 'Hmisc' was built under R version 4.4.2
Attaching package: 'Hmisc'
The following objects are masked from 'package:dplyr':
src, summarize
The following objects are masked from 'package:base':
format.pval, units
# Read the CSV filelibrary(readr)nCounter_Vera <-read_csv("nCounter_Vera.csv")
Rows: 3075 Columns: 45
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (9): HHKID, Visit, Subject Id, Gender, Age Group, FC ID, Date V0, Date ...
dbl (25): Age, Date Diff, C2i_No, Code, ACE, ACKR2, ACKR3, ACKR4, ACOX1, ACS...
lgl (11): Inclusion, Family Cohort, ECG V0, Lab V0, Hair V1, DXA V1, Fit Bit...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 2073 Columns: 2
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): Genes, Pathway
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
combined_data <- long %>%left_join(LongAnnotation, by ="Genes")
Warning in left_join(., LongAnnotation, by = "Genes"): Detected an unexpected many-to-many relationship between `x` and `y`.
ℹ Row 10 of `x` matches multiple rows in `y`.
ℹ Row 8 of `y` matches multiple rows in `x`.
ℹ If a many-to-many relationship is expected, set `relationship =
"many-to-many"` to silence this warning.
# Convert Age_Group to a factorcombined_data$`Age Group`<-as.factor(combined_data$`Age Group`)# Create a pivot table for the heatmapheatmap_data <- combined_data %>%dcast(Pathway ~`Age Group`, value.var ="Measurements", fun.aggregate = mean, na.rm =TRUE)# Melt the data for ggplot2heatmap_data_melt <-melt(heatmap_data, id.vars ="Pathway")# Plot the heatmapggplot(heatmap_data_melt, aes(x = variable, y = Pathway, fill = value)) +geom_tile() +scale_fill_gradient(low ="white", high ="black") +labs(title ="Heatmap of Measurements by Age Group and Pathway",x ="Age Group",y ="Pathway",fill ="Measurements") +theme_minimal() +theme(axis.text.x =element_text(angle =90, hjust =1))
# Log-transform the Measurementscombined_data$Log_Measurements <-log(combined_data$Measurements)# Create a pivot table for the heatmapheatmap_data <- combined_data %>%dcast(Pathway ~`Age Group`, value.var ="Log_Measurements", fun.aggregate = mean, na.rm =TRUE)# Melt the data for ggplot2heatmap_data_melt <-melt(heatmap_data, id.vars ="Pathway")# Plot the heatmapggplot(heatmap_data_melt, aes(x = variable, y = Pathway, fill = value)) +geom_tile() +scale_fill_gradient(low ="white", high ="black") +labs(title ="Heatmap of Log-Transformed Measurements by Age Group and Pathway",x ="Age Group",y ="Pathway",fill ="Log Measurements") +theme_minimal() +theme(axis.text.x =element_text(angle =90, hjust =1, vjust =0.2, size =6), axis.title =element_text(size =8),axis.text.y =element_text(size =6), legend.title =element_text(size =7))
library(ggplot2)library(ggpubr)library(dplyr)# Calculate Spearman's Rank Correlation and p-value by pathwayspearman_results <- combined_data %>%group_by(Pathway) %>%summarise(spearman_corr =cor(Age, Measurements, method ="spearman"),p_val =tryCatch(cor.test(Age, Measurements, method ="spearman")$p.value, warning =function(w) NA) )# Create the regression plot with smaller legend key sizeggplot(combined_data, aes(x = Age, y = Measurements, color = Pathway)) +geom_point(size =1, alpha =0.6) +# Increase point size and add transparencygeom_smooth(method ="lm", se =FALSE, linetype ="dashed", size =1) +# Customize smoothing linestat_cor(method ="spearman", label.x.npc ="left", label.y.npc ="top", size =1.5) +# Adjust correlation label sizelabs(title ="Regression Plot with Spearman's Rank Correlation by Pathway",x ="Age",y ="Measurements",color ="Pathway") +theme_minimal() +theme(axis.text.x =element_text(angle =90, hjust =1, vjust =0.2, size =8), # Adjust axis text sizeaxis.title =element_text(size =10),axis.text.y =element_text(size =8), legend.title =element_text(size =9),legend.key.size =unit(0.055, 'cm'), # Adjust legend key sizeplot.title =element_text(size =12, face ="bold")) # Adjust plot title size and style
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
`geom_smooth()` using formula = 'y ~ x'
#Load the necessary libraries:library(ggplot2)library(dplyr)#Read in your datasets (assuming you have already joined them as combined_data):combined_data$Age <-as.numeric(combined_data$Age)#Loop through each pathway and create regression plots:pathways <-unique(combined_data$Pathway)for (pathway in pathways) { pathway_data <- combined_data %>%filter(Pathway == pathway)# Calculate Spearman's Rank Correlation and p-value, suppressing warnings spearman_test <-suppressWarnings(cor.test(pathway_data$Age, pathway_data$Measurements, method ="spearman")) spearman_corr <- spearman_test$estimate p_value <- spearman_test$p.value# Create regression plot with sex information plot <-ggplot(pathway_data, aes(x = Age, y = Measurements, color = Sex)) +geom_point(size =0.5) +geom_smooth(method ="lm", se =FALSE, size =1.3) +annotate("text", x =Inf, y =Inf, label =paste("Spearman's rho:", round(spearman_corr, 2), "\np-value:", round(p_value, 4)), hjust =1.1, vjust =1.1, size =3, color ="red") +scale_color_manual(values =c("Male"="dodgerblue", "Female"="firebrick2")) +labs(title =paste("Regression Plot for Pathway:", pathway),x ="Age",y ="Measurements") +theme_minimal()print(plot)}
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
combined_data$Age <-as.numeric(combined_data$Age)combined_data$Log_Measurements <-log(combined_data$Measurements)# Loop through each pathway and create regression plotspathways <-unique(combined_data$Pathway)for (pathway in pathways) { pathway_data <- combined_data %>%filter(Pathway == pathway)# Calculate Spearman's Rank Correlation and p-value, suppressing warnings spearman_test <-suppressWarnings(cor.test(pathway_data$Age, pathway_data$Log_Measurements, method ="spearman")) spearman_corr <- spearman_test$estimate p_value <- spearman_test$p.value# Create regression plot with sex information plot <-ggplot(pathway_data, aes(x = Age, y = Log_Measurements, color = Sex)) +geom_point(size =0.5) +geom_smooth(method ="lm", se =FALSE, size =1.3) +annotate("text", x =Inf, y =Inf, label =paste("Spearman's rho:", round(spearman_corr, 2), "\np-value:", round(p_value, 4)), hjust =1.1, vjust =1.1, size =3, color ="red") +scale_color_manual(values =c("Male"="dodgerblue", "Female"="firebrick2")) +labs(title =paste("Regression Plot for Pathway:", pathway),x ="Age",y ="Log Measurements") +theme_minimal()print(plot)}
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
genes <-unique(combined_data$Genes)for (gene in genes) { gene_data <- combined_data %>%filter(Genes == gene)# Calculate Spearman's Rank Correlation and p-value, suppressing warnings spearman_test <-suppressWarnings(cor.test(gene_data$Age, gene_data$Measurements, method ="spearman")) spearman_corr <- spearman_test$estimate p_value <- spearman_test$p.value# Create regression plot with sex information plot <-ggplot(gene_data, aes(x = Age, y = Measurements, color = Sex)) +geom_point(size =0.5) +geom_smooth(method ="lm", se =FALSE, size =1.3) +annotate("text", x =Inf, y =Inf, label =paste("Spearman's rho:", round(spearman_corr, 2), "\np-value:", round(p_value, 4)), hjust =1.1, vjust =1.1, size =3, color ="red") +scale_color_manual(values =c("Male"="dodgerblue", "Female"="firebrick2")) +labs(title =paste("Regression Plot for Gene:", gene),x ="Age",y ="Measurements") +theme_minimal()print(plot)}
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
combined_data$Age <-as.numeric(combined_data$Age)combined_data$Log_Measurements <-log(combined_data$Measurements)# Loop through each gene and create regression plotsgenes <-unique(combined_data$Genes)for (gene in genes) { gene_data <- combined_data %>%filter(Genes == gene)# Calculate Spearman's Rank Correlation and p-value, suppressing warnings spearman_test <-suppressWarnings(cor.test(gene_data$Age, gene_data$Log_Measurements, method ="spearman")) spearman_corr <- spearman_test$estimate p_value <- spearman_test$p.value# Create regression plot with sex information plot <-ggplot(gene_data, aes(x = Age, y = Log_Measurements, color = Sex)) +geom_point(size =0.5) +geom_smooth(method ="lm", se =FALSE, size =1.3) +annotate("text", x =Inf, y =Inf, label =paste("Spearman's rho:", round(spearman_corr, 2), "\np-value:", round(p_value, 4)), hjust =1.1, vjust =1.1, size =3, color ="red") +scale_color_manual(values =c("Male"="dodgerblue", "Female"="firebrick2")) +labs(title =paste("Regression Plot for Gene:", gene),x ="Age",y ="Log Measurements") +theme_minimal()print(plot)}
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
# Count the number of Female and Male for each pathwaycount_female <- combined_data %>%filter(Sex =="Female") %>%nrow()count_male <- combined_data %>%filter(Sex =="Male") %>%nrow()# Calculate Spearman's rank correlation coefficient for Female and Malefemale_data <- combined_data %>%filter(Sex =="Female")male_data <- combined_data %>%filter(Sex =="Male")for (pathway in pathways) { pathway_data <- combined_data %>%filter(Pathway == pathway)if (nrow(female_data) >0) { spearman_female <-rcorr(female_data$Age, female_data$Measurements, type ="spearman") r_female <- spearman_female$r[1, 2] p_female <- spearman_female$P[1, 2] } else { r_female <-NA p_female <-NA }if (nrow(male_data) >0) { spearman_male <-rcorr(male_data$Age, male_data$Measurements, type ="spearman") r_male <- spearman_male$r[1, 2] p_male <- spearman_male$P[1, 2] } else { r_male <-NA p_male <-NA }# Create the regression plot plot <-ggplot(pathway_data, aes(x = Age, y = Measurements, color = Sex)) +geom_point(position =position_jitter(width =0.1), alpha =0.5) +geom_smooth(method ="loess", se =FALSE) +labs(title =paste("Regression Plot of", pathway, "by Gender"),subtitle =paste("n (Female) =", count_female, ", n (Male) =", count_male),x ="Age",y ="Measurements in log10 scale") +scale_y_log10() +scale_color_manual(values =c("Female"="#ff7070", "Male"="#6495ED")) +# Set colorstheme_bw() +annotate("text", x =max(pathway_data$Age, na.rm =TRUE) *0.8, y =max(pathway_data$Measurements, na.rm =TRUE) *1.1, label =paste("Female: R =", round(r_female, 2), ", p =", format(p_female, scientific =TRUE)), hjust =0.5, vjust =1, size =3, color ="black") +annotate("text", x =max(pathway_data$Age, na.rm =TRUE) *0.8, y =max(pathway_data$Measurements, na.rm =TRUE) *1.05, label =paste("Male: R =", round(r_male, 2), ", p =", format(p_male, scientific =TRUE)), hjust =0.5, vjust =2, size =3, color ="black")print(plot)}
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
# Count the number of Female and Male for each pathwaycount_female <- combined_data %>%filter(Sex =="Female") %>%nrow()count_male <- combined_data %>%filter(Sex =="Male") %>%nrow()# Calculate Spearman's rank correlation coefficient for Female and Malefemale_data <- combined_data %>%filter(Sex =="Female")male_data <- combined_data %>%filter(Sex =="Male")for (gene in genes) { gene_data <- combined_data %>%filter(Genes == gene)if (nrow(female_data) >0) { spearman_female <-rcorr(female_data$Age, female_data$Measurements, type ="spearman") r_female <- spearman_female$r[1, 2] p_female <- spearman_female$P[1, 2] } else { r_female <-NA p_female <-NA }if (nrow(male_data) >0) { spearman_male <-rcorr(male_data$Age, male_data$Measurements, type ="spearman") r_male <- spearman_male$r[1, 2] p_male <- spearman_male$P[1, 2] } else { r_male <-NA p_male <-NA }# Create the regression plot plot <-ggplot(gene_data, aes(x = Age, y = Measurements, color = Sex)) +geom_point(position =position_jitter(width =0.1), alpha =0.5) +geom_smooth(method ="loess", se =FALSE) +labs(title =paste("Regression Plot of", gene, "by Gender"),subtitle =paste("n (Female) =", count_female, ", n (Male) =", count_male),x ="Age",y ="Measurements in log10 scale") +scale_y_log10() +scale_color_manual(values =c("Female"="#ff7070", "Male"="#6495ED")) +# Set colorstheme_bw() +annotate("text", x =max(gene_data$Age, na.rm =TRUE) *0.8, y =max(gene_data$Measurements, na.rm =TRUE) *1.1, label =paste("Female: R =", round(r_female, 2), ", p =", format(p_female, scientific =TRUE)), hjust =0.5, vjust =1, size =3, color ="black") +annotate("text", x =max(gene_data$Age, na.rm =TRUE) *0.8, y =max(gene_data$Measurements, na.rm =TRUE) *1.05, label =paste("Male: R =", round(r_male, 2), ", p =", format(p_male, scientific =TRUE)), hjust =0.5, vjust =2, size =3, color ="black")print(plot)}