# Load required packages
library(tidyverse)
## Warning: 程辑包'tidyverse'是用R版本4.2.3 来建造的
## Warning: 程辑包'ggplot2'是用R版本4.2.3 来建造的
## Warning: 程辑包'tibble'是用R版本4.2.3 来建造的
## Warning: 程辑包'tidyr'是用R版本4.2.3 来建造的
## Warning: 程辑包'readr'是用R版本4.2.3 来建造的
## Warning: 程辑包'purrr'是用R版本4.2.3 来建造的
## Warning: 程辑包'dplyr'是用R版本4.2.3 来建造的
## Warning: 程辑包'stringr'是用R版本4.2.3 来建造的
## Warning: 程辑包'forcats'是用R版本4.2.3 来建造的
## Warning: 程辑包'lubridate'是用R版本4.2.3 来建造的
## -- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --
## v dplyr 1.1.4 v readr 2.1.5
## v forcats 1.0.0 v stringr 1.5.1
## v ggplot2 3.4.2 v tibble 3.2.1
## v lubridate 1.9.3 v tidyr 1.3.1
## v purrr 1.0.2
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggpubr)
library(gridExtra)
##
## 载入程辑包:'gridExtra'
##
## The following object is masked from 'package:dplyr':
##
## combine
# Create sample data
data_4 <- data.frame(
key = rep(1:5, 2),
value = c(rnorm(5, 10, 2), rnorm(5, 12, 2)),
Sample.Name = rep(c("Sample A", "Sample B"), each = 5)
)
data_4
## key value Sample.Name
## 1 1 12.335172 Sample A
## 2 2 11.577934 Sample A
## 3 3 10.693085 Sample A
## 4 4 9.401919 Sample A
## 5 5 11.431701 Sample A
## 6 1 11.076065 Sample B
## 7 2 8.594319 Sample B
## 8 3 13.676133 Sample B
## 9 4 9.786103 Sample B
## 10 5 9.594805 Sample B
# Define pastel color palette
pastel_colors <- c("#FFB3BA", "#BAFFC9") # Colors for Sample A and B
pastel_fit_color <- "#B5D8EB" # Pastel blue for fitting line
# Alternative pastel colors for fitting line:
# "#E0BBE4" (Pastel purple)
# "#FEC8D8" (Pastel pink)
# "#FFDFD3" (Pastel orange)
# Create base theme
my_theme <- theme(
# Panel settings
panel.spacing = unit(0.1, "cm"),
panel.background = element_rect(fill = "white", colour = "white"),
panel.grid.major = element_line(size = 0.5, linetype = 'dashed', colour = "#F0F0F0"),
# Legend settings
legend.position = "right",
legend.title = element_text(size=14),
legend.text = element_text(size=14),
legend.background = element_rect(fill = "white", color = NA),
# Axis settings
axis.ticks = element_line(colour = "#666666", size = 0.5),
axis.line = element_line(colour = "#666666", size = 0.5),
axis.text = element_text(
face="plain",
color="#666666",
family = "sans",
size=14
),
axis.title = element_text(
color="#666666",
size=14,
face="plain",
family="sans"
),
# Title settings
plot.title = element_text(
size = 16,
hjust = 0.5,
margin = margin(b = 20)
)
)
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## i Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Create Linear Regression plot
p1 <- ggplot(data_4, aes(x=key, y=value)) +
# Add lines and points for original data
geom_line(aes(group=Sample.Name, color=Sample.Name),
size=0.8,
alpha = 0.7) +
geom_point(aes(color=Sample.Name),
size=2.5) +
# Add fitting line
geom_smooth(method = "lm",
color = pastel_fit_color,
size = 1.2,
se = TRUE, # Show confidence interval
alpha = 0.2, # Transparency of confidence interval
aes(group = 1)) + # Single fitting line for all data
# Add correlation coefficient
stat_cor(aes(group = 1),
label.x = 1.5,
label.y = max(data_4$value) + 1,
color = pastel_fit_color,
size = 4,
method = "pearson",
label.sep = "\n") +
# Apply color scheme
scale_color_manual(values = pastel_colors) +
ggtitle("Linear Regression (LM)") +
my_theme
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## i Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Create LOESS Regression plot
p2 <- ggplot(data_4, aes(x=key, y=value)) +
# Add lines and points for original data
geom_line(aes(group=Sample.Name, color=Sample.Name),
size=0.8,
alpha = 0.7) +
geom_point(aes(color=Sample.Name),
size=2.5) +
# Add LOESS fitting line
geom_smooth(method = "loess",
color = pastel_fit_color,
size = 1.2,
se = TRUE, # Show confidence interval
alpha = 0.2, # Transparency of confidence interval
aes(group = 1)) + # Single fitting line for all data
# Add correlation coefficient
stat_cor(aes(group = 1),
label.x = 1.5,
label.y = max(data_4$value) + 1,
color = pastel_fit_color,
size = 4,
method = "pearson",
label.sep = "\n") +
# Apply color scheme
scale_color_manual(values = pastel_colors) +
ggtitle("Local Regression (LOESS)") +
my_theme
# Arrange both plots side by side
grid.arrange(p1, p2, ncol=2)
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'

ggsave(filename = paste0(Sys.Date(),"-","cc=1_p2.tif"),
plot = last_plot(), device = "tiff",
width = 14, height = 14, units = "cm",
dpi = 300, limitsize = TRUE, compression = "lzw")
## `geom_smooth()` using formula = 'y ~ x'