R语言ggplot2漂亮的热图和配色简单小例子

原创
2021/04/14 17:22
阅读数 3K

偶然间在github 上发现的这个链接,示例数据和代码都有,很好的R语言学习素材 链接是 https://github.com/blmoore/blogR ,主要内容有

image.png

光看这个可能有些枯燥,我们来看结果图

image.png
image.png
image.png
image.png
image.png
image.png

这些看起来是不是还挺酷炫的,接下来的推文争取把这些图片对应的代码全部重复一遍,今天重复这个热图

这个图具体的数据是什么意思暂时还没太看明白,最终用于作图的数据格式如下

image.png

前半部分准备数据的代码这里就不介绍了

image.png

准备数据的过程可能稍微有点枯燥,大家感兴趣的话可以自己研究研究

我们直接运行画图代码

加载ggplot2
library(ggplot2)
最基本的热图
ggplot(mdf, aes(y=state, x=year, fill=c)) + 
  geom_tile()
image.png
调整热图方块的一些内容
ggplot(mdf, aes(y=state, x=year, fill=c)) + 
  geom_tile(colour="white"
            #linewidth=2, 
            width=.9, 
            height=.9)
image.png

这里原来热图对应的小单元格高和宽是都可以调整的

调整热图的颜色和图例
ggplot(mdf, aes(y=state, x=year, fill=c)) + 
  geom_tile(colour="white"
            #linewidth=2, 
            width=.9, 
            height=.9)+
  theme_minimal()+
  scale_fill_gradientn(colours=cols, limits=c(0, 4000),
                       breaks=seq(0, 4e3, by=1e3), 
                       na.value=rgb(246, 246, 246, max=255),
                       labels=c("0k""1k""2k""3k""4k"),
                       guide=guide_colourbar(ticks=T, 
                                             nbin=50,
                                             barheight=.5, 
                                             label=T,
                                             barwidth=10))+
  theme(legend.position = "top")
image.png

最终的结果

ggplot(mdf, aes(y=state, x=year, fill=c)) + 
  geom_tile(colour="white"
            #linewidth=2, 
            width=.9, 
            height=.9)+
  theme_minimal()+
  scale_fill_gradientn(colours=cols, limits=c(0, 4000),
                       breaks=seq(0, 4e3, by=1e3), 
                       na.value=rgb(246, 246, 246, max=255),
                       labels=c("0k""1k""2k""3k""4k"),
                       guide=guide_colourbar(ticks=T, 
                                             nbin=50,
                                             barheight=.5, 
                                             label=T,
                                             barwidth=10))+
  #theme(legend.position = "top")+
  scale_x_continuous(expand=c(0,0), 
                     breaks=seq(1930, 2010, by=10)) +
  geom_segment(x=1963, xend=1963, y=0, yend=51.5, size=.9, lineend = "round") +
  labs(x="", y="", fill="") +
  ggtitle("Measles") +
  theme(legend.position=c(.5, -.13),
        legend.direction="horizontal",
        legend.text=element_text(colour="grey20"),
        plot.margin=grid::unit(c(.5,.5,1.5,.5), "cm"),
        axis.text.y=element_text(size=6, 
                                 #family="Helvetica", 
                                 hjust=1),
        axis.text.x=element_text(size=8),
        axis.line.x=element_line(colour="grey20"
                                 size=0.5),
        #axis.ticks.y=element_blank(),
        #axis.ticks.x=element_blank(),
        #axis.ticks.length=grid::unit(1, "cm"),
        panel.grid=element_blank(),
        title=element_text(hjust=-.07, 
                           face="bold"
                           vjust=1)) +
  annotate("text", label="Vaccine introduced",
           x=1963, y=53, vjust=1, hjust=0,
           size=I(3))
image.png

这个热图最终还挺漂亮的

欢迎大家关注我的公众号

小明的数据分析笔记本

小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!


本文分享自微信公众号 - 小明的数据分析笔记本(gh_0c8895f349d3)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部