ggplot2 | line plot 分组及均值线:聚类后的表达变化趋势图

1. 效果图

2. 预处理及绘图

复制代码
# 输入数据
> head(dat)
  Species cid     variable value
1  setosa   1 Sepal.Length   5.1
2  setosa   2 Sepal.Length   4.9
3  setosa   3 Sepal.Length   4.7
4  setosa   4 Sepal.Length   4.6
5  setosa   5 Sepal.Length   5.0
6  setosa   6 Sepal.Length   5.4


# 预处理及绘图
row.num=nrow(iris)
dat=iris[1:row.num,] #假设有4个时间点,最后一列是聚类结果
dat$cid=rownames(dat) #很重要!用于 ggplot 的 group
head(dat)
#宽变长
dat = tidyr::gather(data = dat, key = "variable", value = "value", -c(Species, cid))
# or: 宽变长
#dat=reshape2::melt(dat)

# 限定x轴顺序
dat$variable=factor(dat$variable, levels = c('Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width'))
head(dat)
dim(dat)
ggplot(dat, aes(x=variable, y=value, group=cid, color=Species))+
  geom_hline(yintercept =4, linetype=2, size=0.5) +
  geom_line(size=0.3)+
  stat_summary(aes(group=1),fun.y=mean, geom="line", size=0.8, color="black")+
  facet_wrap(Species~.)+
  labs(x="Stage", y='Expression')+
  theme_bw()+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        axis.text = element_text(size=8, face = "bold"),
        axis.text.x=element_text(angle=60, hjust=1),
        strip.background = element_rect(fill="white"),
        strip.text = element_text(size = 8, face = "bold"))
相关推荐
生信大表哥21 小时前
贝叶斯共识聚类(BCC)
机器学习·数据挖掘·聚类
大数据魔法师1 天前
Seaborn(一) - Seaborn绘图方法介绍
matplotlib·数据可视化
CV实验室2 天前
AAAI 2026 Oral 之江实验室等提出MoEGCL:在6大基准数据集上刷新SOTA,聚类准确率最高提升超8%!
人工智能·机器学习·计算机视觉·数据挖掘·论文·聚类
StarPrayers.3 天前
K-means 聚类
机器学习·kmeans·聚类
一晌小贪欢4 天前
【Python数据分析】数据分析与可视化
开发语言·python·数据分析·数据可视化·数据清洗
面向星辰5 天前
k均值,密度聚类,层次聚类三种聚类底层逻辑的区别
机器学习·均值算法·聚类
招风的黑耳5 天前
Axure可视化大屏原型模板库:学习设计/提高效率/快速可视化
学习·axure·数据可视化·大屏设计
Learn Beyond Limits5 天前
Clustering vs Classification|聚类vs分类
人工智能·算法·机器学习·ai·分类·数据挖掘·聚类
余衫马6 天前
聚类算法入门:像魔法一样把数据自动归类
人工智能·算法·机器学习·聚类
合作小小程序员小小店7 天前
web网页开发,在线%聚类,微博,舆情%系统,基于python,pycharm,django,nlp,kmeans,mysql
python·pycharm·kmeans·聚类·sklearn·kmean