library(readr)
# 加载数据
df <- read_delim("all_length.tsv", delim = "\t", col_names = TRUE)
# 移除所有包含 "INV" 的行
df_filtered <- df %>%
filter(type != "INV")
df<-df_filtered
# 查看过滤后的数据
head(df)
library(ggplot2)
df_summary <- df %>%
mutate(binned_length = cut(length, breaks = c(0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500))) %>%
group_by(type, group, binned_length) %>%
summarise(count = n()) %>%
ungroup()
# 查看数据
head(df_summary)
write.csv(df_summary, file = "df_summary.csv", row.names = FALSE)
df_summary <- df %>%
mutate(binned_length = cut(length, breaks = c(0, 100, 200, 300, 400, 500,600,700,800,900,1000,5000,10000,100000))) %>%
group_by(type, group, binned_length) %>%
summarise(count = n()) %>%
ungroup()
p=ggplot(df_summary, aes(x = binned_length, y = count, color = type, linetype = group, group = interaction(type, group))) +
geom_line(size = 1) +
labs(title = "Length Distribution by Type and Group",
x = "Length Interval",
y = "Count",
color = "Type",
linetype = "Group") +
scale_color_manual(values = c("INS" = "blue", "DEL" = "red", "DUP" = "green")) +
scale_linetype_manual(values = c("Xian" = "solid", "Geng" = "dashed")) +
theme_minimal()
p
# 保存图
ggsave("length_distribution.png", plot = p, width = 15, height = 8, dpi = 300)
# 计算每个类型、组别和长度区间内的平均值和标准差
df_summary_avg <- df %>%
mutate(binned_length = cut(length, breaks = c(0, 100, 200, 300, 400, 500,600,700,800,900,1000,5000,10000,100000))) %>%
group_by(type, group, binned_length) %>%
summarise(
mean_length = round(mean(length, na.rm = TRUE)),
sd_length = round(sd(length, na.rm = TRUE))
) %>%
ungroup()
# 查看结果
df_summary_avg
write.csv(df_summary_avg, file = "df_summary_avg.csv", row.names = FALSE)
不分大的Group
setwd("D:\\科研助理工作\\data\\9-2")
data <- read.table("sv_length.csv", sep = ",", header = TRUE)
data$SVLEN <- abs(as.integer(data$SVLEN))
head(data)
library(dplyr)
# 计算每种 SVTYPE 的均值和标准差
summary_data <- data %>%
group_by(SVTYPE) %>%
summarise(mean_SVLEN = mean(SVLEN),
sd_SVLEN = sd(SVLEN))
print(summary_data)
write.csv(summary_data, "sv_summary.csv", row.names = FALSE)
library(ggplot2)
# 定义分组区间
intervals <- c(0, 100, 500, 1000, 5000, 10000, 20000, 50000, 100000, 300000, 500000, Inf)
labels <- c("<100", "101-500", "501-1000", "1001-5000", "5001-10000", "10001-20000", "20001-50000", "50001-100000", "100001-300000", "300001-500000", ">500000")
result <- data %>%
mutate(group = cut(SVLEN, breaks = intervals, labels = labels, right = FALSE)) %>%
group_by(SVTYPE, group) %>%
summarise(mean_length = mean(SVLEN), sd_length = sd(SVLEN), count = n()) %>%
mutate(mean_length = as.integer(mean_length), sd_length = as.integer(sd_length))
print(result)
# 保存数据为 CSV 文件
write.csv(result, "sv_grouped_stat.csv", row.names = FALSE)
# 定义分组区间
intervals <- c(0, 100, 500, 1000, 5000, 10000, 20000, 50000, 100000, 300000, 500000, Inf)
labels <- c("<100", "101-500", "501-1000", "1001-5000", "5001-10000", "10001-20000", "20001-50000", "50001-100000", "100001-300000", "300001-500000", ">500000")
result <- data %>%
mutate(group = cut(SVLEN, breaks = intervals, labels = labels, right = FALSE)) %>%
group_by(SVTYPE, group) %>%
summarise(count = n())
# 绘制折线图
ggplot(result, aes(x = group, y = count, color = SVTYPE)) +
geom_line() +
geom_point() +
labs(x = "SV Length Group", y = "Count", title = "SV Length Distribution by SVTYPE", color = "SVTYPE") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# 定义分组区间
intervals <- c(0, 100, 500, 1000, 5000, 10000, 20000, 50000, 100000, 300000, 500000, Inf)
labels <- c("<100", "101-500", "501-1000", "1001-5000", "5001-10000", "10001-20000", "20001-50000", "50001-100000", "100001-300000", "300001-500000", ">500000")
result <- data %>%
mutate(group = cut(SVLEN, breaks = intervals, labels = labels, right = FALSE)) %>%
group_by(SVTYPE, group) %>%
summarise(count = n())
# 如果存在组只有一个观测值的情况,可以考虑使用柱状图
ggplot(result, aes(x = group, y = count, fill = SVTYPE)) +
geom_bar(stat = "identity", position = "dodge") +
labs(x = "SV Length Group", y = "Count", title = "SV Length Distribution by SVTYPE", fill = "SVTYPE") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# 使用新的分组区间
intervals <- c(0, 100, 200, 300, 400, 500, 600, 700, 800, 900,1000,5000,10000,100000,300000)
labels <- c("0-100", "101-200", "201-300", "301-400", "401-500", "501-600", "601-700",
"701-800","801-900","901-1000","1001-5000","5001-10000","10001-100000","100001-300000")
result <- data %>%
mutate(group = cut(SVLEN, breaks = intervals, labels = labels, right = FALSE)) %>%
group_by(SVTYPE, group) %>%
summarise(count = n())
# 绘制折线图
ggplot(result, aes(x = group, y = count, group = SVTYPE, color = SVTYPE)) +
geom_line() +
geom_point() +
labs(x = "SV Length Group", y = "Count", title = "SV Length Distribution by SVTYPE", color = "SVTYPE") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# 绘制折线图
p=ggplot(result, aes(x = group, y = count, group = SVTYPE, color = SVTYPE)) +
geom_line(size = 1.2) +
geom_point() +
labs(x = "SV Length Group", y = "Count", title = "SV Length Distribution by SVTYPE", color = "SVTYPE") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1),
# axis.line = element_blank(), # 去掉坐标轴实线
panel.grid.major = element_blank(), # 去掉主要网格线
panel.grid.minor = element_blank()) + # 去掉次要网格线
scale_y_continuous(breaks = seq(0, max(result$count), by = 5000)) # 根据数据调整 y 轴刻度
ggsave("length_distribution1.png", plot = p, width = 15, height = 8, dpi = 300)
SV-dis