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"))
相关推荐
大数据魔法师9 小时前
Matplotlib(一)- 数据可视化与Matplotlib
matplotlib·数据可视化
gis收藏家21 小时前
等大小谱聚类
机器学习·支持向量机·聚类
计算生物前沿2 天前
单细胞分析教程 | (二)标准化、特征选择、降为、聚类及可视化
人工智能·机器学习·聚类
李昊哲小课2 天前
销售数据可视化分析项目
python·信息可视化·数据分析·matplotlib·数据可视化·seaborn
bug攻城狮4 天前
Alloy VS Promtail:基于 Loki 的日志采集架构对比与选型指南
运维·架构·grafana·数据可视化
今禾6 天前
" 当Base64遇上Blob,图像转换不再神秘,让你的网页瞬间变身魔法画布! "
前端·数据可视化
JIAKSK7 天前
VitePress 接入百度统计:全面教程与优化指南
运维·数据可视化
datagear8 天前
如何在DataGear 5.4.1 中快速制作HTTP数据源服务端分页的数据表格看板
javascript·数据可视化
wenzhangli78 天前
OneCode 图表组件核心优势解析
数据可视化
镜舟科技9 天前
StarRocks × Tableau 连接器完整使用指南 | 高效数据分析从连接开始
starrocks·数据分析·数据可视化·tableau·连接器·交互式分析·mpp 数据库