1. 数据格式
2. 运行代码
library(tidyverse)
library(ggpubr) # 如果后面想加显著性,可顺手载入
# 1. 读入数据
df <- read.delim("E:/Retrogene课题/小孢子叶球注射过表达基因结果/统计.txt",
header = TRUE)
df_long <- df %>%
pivot_longer(cols = -gene_id, names_to = "Category", values_to = "Count") %>%
group_by(gene_id) %>%
mutate(
percent = Count / sum(Count, na.rm = TRUE),
label = scales::percent(percent, accuracy = 0.1)
) %>%
ungroup()
# 3. Nature风格配色(可自定)
nature_colors <- c("Higher" = "#4C72B0", "Lower" = "#55A868", "Unchanged" = "#C44E52")
# 4. 绘图
p <- ggplot(df_long, aes(x = gene_id, y = percent, fill = Category)) +
geom_col(color = "black", width = 0.7) +
geom_text(aes(label = label),
position = position_stack(vjust = 0.5),
color = "white", size = 3) +
scale_y_continuous(labels = scales::percent_format()) +
scale_fill_manual(values = c(
"Higher" = "#4C72B0",
"Lower" = "#55A868",
"Unchanged" = "#C44E52"
)) +
labs(x = "Gene ID", y = NULL, fill = NULL) +
theme_classic(base_size = 12, base_family = "sans") +
theme(
axis.text.x = element_text(angle = 0, size = 10),
legend.position = "right"
)
ggsave("E:/Proportional Stacked Bar Chart.png", plot = p, width = 12, height = 8, dpi = 300) #保存图片
3. 结果文件
4. 备注
R语言中和windows中 对于字体的识别不同
windowsFonts() # 查看可用字体