ixxmu / mp_duty

抓取网络文章到github issues保存
https://archives.duty-machine.now.sh/
116 stars 30 forks source link

GGplot绘制个性化热图 #3562

Closed ixxmu closed 1 year ago

ixxmu commented 1 year ago

https://mp.weixin.qq.com/s/unByZmlVicTB9EE74GbhOA

ixxmu commented 1 year ago

GGplot绘制个性化热图 by 单细胞天地


分享是一种态度

本文内容:

  • ggplot2绘制热图
  • 热图展示方格为方块
  • 热图分面、添加标记
  • 换行长字符
  • 配色
  • 热图展示为点图

加载R包

pkgs = c('tidyverse''forcats''gtools''ggplot2''ggpubr''cowplot'
         'scales''ggsci''viridis''hrbrthemes''Cairo''common')
# install.packages('pkgs')
inst = lapply(pkgs, library, character.only = TRUE)

读取或创建数据

strings <- paste0('C', 1:5)
data <- data.frame(
day = 1:12,
type.name = c(rep(c('A', 'B'), each = 6)),
condition = c('Control Group', 'Experimental Group1', 'Experimental Group2'),
Dilution = rep(c(1:2), each = 3),
Dilution1 = sample(strings, 12, replace = T),
Dilution2 = sample(strings, 12, replace = T),
Dilution3 = sample(strings, 12, replace = T)
)

data %>%
slice(1:3)

绘图

1、绘制热图

p <- data %>%
  gather(key, value, paste0('Dilution', 1:3)) %>%
  unite(condition, condition, Dilution, sep = ' ') %>%
  ggplot(aes(x = key, y = condition)) +
  geom_tile(aes(fill = value), color = 'white', size = 1)
p

2、调整方格为方块

p1 <- p + coord_fixed() 
p1

3、热图分面

p2 <- p1 + facet_wrap(~type.name) 
p2

4、热图添加标记

p3 <- p2 + geom_text(aes(label = value), color = 'black', size = 4)
p3

5、字符换行

p4 <- p3 + scale_y_discrete(labels = label_wrap(10))
p4

6、配色

p4 + theme_classic2(base_size = 16) + scale_fill_simpsons() +
  theme(axis.text.x = element_text (angle = 45, vjust = 1, hjust=1)) 

7、用点绘制热图

# 用点绘制热图,颜色为分类变量
data %>%
  gather(key, value, paste0('Dilution', 1:3)) %>%
  unite(condition, condition, Dilution, sep = ' ') %>%
  ggplot(aes(x = key, y = condition)) +
  coord_fixed() +
  facet_wrap(~ type.name) +
  geom_point(aes(size = value, color = value)) +
  theme_bw() +
  theme(panel.border = element_rect(fill=NA, color="black", size=1, linetype="solid")) + 
  theme(panel.grid = element_blank(),
        axis.text.x =element_text(angle =90, hjust =0.5, vjust = 0.5)) +
  theme_classic2(base_size = 16) + 
  scale_color_simpsons() +
  theme(axis.text.x = element_text (angle = 45, vjust = 1, hjust=1))

8、用点绘制热图2

# 用点绘制热图,颜色为连续变量
data.d <- data %>%
  gather(key, value, paste0('Dilution', 1:3)) %>%
  unite(condition, condition, Dilution, sep = ' ') %>%
  # charactor to factor, factor to numeric
  mutate(value = as.numeric(factor(value)))

data.d %>%
  ggplot(aes(x = key, y = condition)) + 
  coord_fixed() +
  facet_wrap(~ type.name) +
  geom_point(aes(size = value, color = value)) +
  theme_bw() +
  theme(panel.border = element_rect(fill=NA, color="black", size=1, linetype="solid")) + 
  theme(panel.grid = element_blank(),
        axis.text.x =element_text(angle =90, hjust =0.5, vjust = 0.5)) +
  theme_classic2(base_size = 16) + 
  scale_color_gradientn(colours = c('#6699CC','#FFFF99','#CC3333')) +
  theme(axis.text.x = element_text (angle = 45, vjust = 1, hjust=1))

参考

  • [ggplot2绘制个性化热图](https://zhuanlan.zhihu.com/p/442663764)    

  • [ggplot2合并共享坐标轴的图片](https://aosmith.rbind.io/2019/05/13/small-multiples-plot/#using-facets-for-small-multiples)    

  • [Heat map in ggplot2](https://r-charts.com/correlation/heat-map-ggplot2/#:~:text=%23%20install.packages%20%28%22ggplot2%22%29%20library%28ggplot2%29%20ggplot%28df%2C%20aes%28x%20%3D%20x%2C,to%20the%20label%20argument%20of%20the%20aes%20function.)     

  • [Quick and easy ways to deal with long labels in ggplot2](https://www.andrewheiss.com/blog/2022/06/23/long-labels-ggplot/)   


往期回顾

Featureplot美化

单细胞转录组揭示系统性红斑狼疮的特定细胞类型的基因表达变化

倒计时7天 | 百创智造S系列空间转录组细胞分割产品发布会

单细胞多组学技术知多少

手把手带你复现NC图表之Figure6






如果你对单细胞转录组研究感兴趣,但又不知道如何入门,也许你可以关注一下下面的课程



看完记得顺手点个“在看”哦!


生物 | 单细胞 | 转录组丨资料
每天都精彩

长按扫码可关注