Closed ixxmu closed 1 year ago
分享是一种态度
本文内容:
ggplot2绘制热图 热图展示方格为方块 热图分面、添加标记 换行长字符 配色 热图展示为点图
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)
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
p1 <- p + coord_fixed()
p1
p2 <- p1 + facet_wrap(~type.name)
p2
p3 <- p2 + geom_text(aes(label = value), color = 'black', size = 4)
p3
p4 <- p3 + scale_y_discrete(labels = label_wrap(10))
p4
p4 + theme_classic2(base_size = 16) + scale_fill_simpsons() +
theme(axis.text.x = element_text (angle = 45, vjust = 1, hjust=1))
# 用点绘制热图,颜色为分类变量
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))
# 用点绘制热图,颜色为连续变量
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/)
如果你对单细胞转录组研究感兴趣,但又不知道如何入门,也许你可以关注一下下面的课程
看完记得顺手点个“在看”哦!
长按扫码可关注
https://mp.weixin.qq.com/s/unByZmlVicTB9EE74GbhOA