科学网

 找回密码
  注册

tag 标签: ggplot

相关帖子

版块 作者 回复/查看 最后发表

没有相关内容

相关日志

R语言 ggplot2绘制柱状图加误差线
DrD 2020-7-5 21:28
## 构建数据 dat - data.frame(var = c(A,B,C), mean =c (0.25,0.45,0.65), sd = c(0.1,0.15,0.2)) dat var mean sd 1 A 0.25 0.10 2 B 0.45 0.15 3 C 0.65 0.20 ## library(ggplot2) f - ggplot(dat,aes(x=var, y=mean)) + geom_bar(stat = identity, position=position_dodge(), width=0.6, color=red, fill=c(white,grey,black)) + ## 柱状图线条颜色和填充色 geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=0.2, color=blue, ## 误差线颜色 position=position_dodge(0.6)) ## 更改主图 f + theme_bw()
个人分类: R语言学习笔记|13059 次阅读|0 个评论
制作玫瑰图-ggplot2-R
geoRp 2020-3-19 09:44
来自博主个人公众号,原文链接: 在这里 人民日报公众号在疫情期间推送的消息,有几副图大家肯定见了不止一次。 尤其是下边这副,无论是大小、颜色还是视角效果,都很棒,可读性很强! 人民日报的图 想想excel出来的条形图,我不禁打开了尘封了三天的电脑、R和firefox: 极坐标与玫瑰图 玫瑰图可以视为是条形图在极坐标系中的表达 利用ggplot实现的思路也是先构建bar(geom_bar),然后将坐标系转为极坐标系(coord_polar) 下边是官方文档给出的例子: # A coxcomb plot = bar chart + polar coordinates cxc - ggplot(mtcars, aes(x = factor(cyl))) + geom_bar(width = 1 , colour = black ) multiplot(cxc,cxc + coord_polar(),cols= 2 ) 这种思路也可以用在饼状图的制作中,不过饼状图需要以堆积的条形图为基础。 # A pie chart = stacked bar chart + polar coordinates pie - ggplot(mtcars, aes(x = factor( 1 ), fill = factor(cyl))) + geom_bar(width = 1 ) multiplot(pie,pie + coord_polar(theta = y ),cols= 2 ) 理清了大致思路,我们着手进入实践。 实践流程 准备数据,以3月5日的疫情数据为例: head(data0305) ## X 省 省确诊 日期 ## 1 1 广东省 1352 2020-03-05 ## 2 34 河南省 1272 2020-03-05 ## 3 52 浙江省 1215 2020-03-05 ## 4 102 湖南省 1018 2020-03-05 ## 5 126 安徽省 990 2020-03-05 ## 6 154 江西省 935 2020-03-05 首先,构建条形图。并按照确诊人数排序,颜色(渐进色)填充,gg都hold不住的条形图 copl1=ggplot(data0305)+ geom_bar(stat= identity ,aes(y=省确诊,x=reorder(省, 省确诊),fill=省确诊)) #这里要reorder一下,让省按照省确诊来排列,不然参差不齐 copl1 将坐标系换成极坐标系 copl2=ggplot(data0305)+ geom_bar(stat= identity ,aes(y=省确诊,x=reorder(省, 省确诊),fill=省确诊))+ #如上,#这里要reorder一下,让省按照省确诊来排列,不然参差不齐 coord_polar() copl2 看起来有那么点意思了,再调调色试一下 copl2+ scale_fill_gradient(low= 'white' ,high= 'red' )+ #渐变色填充,由白到红 theme_bw() 调整下边框、坐标轴文本、图例 copl= copl2+scale_fill_gradient(low= 'white' ,high= 'red' )+ theme_bw()+ theme(axis.text.y = element_blank(), axis.text.x = element_text(colour = 'black' , face = 'bold' ,size = 9 ), panel.border = element_blank())+ xlab(label = '' )+ylab(label = '' )+ guides(fill= FALSE ) copl 对比下人民日报的原图: 人民日报的图 此图明显不是渐进色的填充,根据不同地区选择了不同颜色;而且标签文本进行了相应的调整,需要耗费不少的精力。 然而,实践中的出图已经达到了神似的水平,为下一步打磨奠定了坚实的基础。。。。 尤其是加上指示标签后,不是吗 copl+geom_text_repel(aes(label = 省, y = 省确诊,x=省),size= 3 )+ theme(axis.text.y = element_blank(), axis.text.x = element_text(colour = 'white' , face = 'bold' ,size = 2 ), panel.border = element_blank())
个人分类: R编程|3596 次阅读|0 个评论
R语言笔记——ggplot2画回归曲线,添加方程或P值
Hunshandake 2020-1-2 15:35
library(ggplot2) library(dplyr) # 加载 dplyr 包 library(ggpmisc) # 加载 ggpmisc 包 library(RColorBrewer) library(ggpubr) # 载入数据,计算均值和 se caomuxi-read.csv(E:/R/Rfiles/data.csv) windowsFonts(SH = windowsFont(Times New Roman)) #第一种方案,做点状图,加回归线,添加回归方程和R 2 a- ggplot(data=data,aes(x=Fert, y=Hight))+ geom_point(aes(color=treatment),size = 3)+ # 设置分组颜色和点的大小 geom_smooth(method = lm,linetype=3,se=FALSE,colour=black,span=0.8)+ # 添加回归曲线, se 取消置信空间, linetype 设置线型 stat_poly_eq(aes(label = paste(..eq.label.., ..adj.rr.label.., sep = '~~~~')), formula = y ~ x, parse = T,family = SH) + # 添加回归方程和 R 2 scale_x_continuous()+ scale_y_continuous(expand = c(0, 0),# 设定 x 轴和 y 轴的交叉点 name =Height (cm),# 设定 y 轴标题 breaks=seq(0,50,10),# 设定 Y 轴的数据间隔 limits = c(10,50) # 设定 Y 轴的数据上下限 )+ scale_color_brewer(palette = YlOrRd)+ theme() a 出图如下: #第二种方案,做点状图,加回归线,添加R 2 和P值 a- ggplot(data=data,aes(x=Fert, y=Hight))+ geom_point(aes(color=treatment),size = 3)+ # 设置分组颜色和点的大小 geom_smooth(method = lm,linetype=3,se=FALSE,colour=black,span=0.8)+ # 添加回归曲线, se 取消置信空间, linetype 设置线型 stat_fit_glance(method = 'lm', method.args = list(formula = y ~ x), mapping = aes(label = sprintf('R^2~=~%.3f~~italic(P)~=~%.2g', stat(r.squared), stat(p.value))), parse = TRUE,label.x = 0.95,label.y = 0.95,family = SH)+ # 方案 2 ,仅添加 R 2 和 P 值, label.x 和 label.y 设置文字位置。 scale_x_continuous()+ scale_y_continuous(expand = c(0, 0),# 设定 x 轴和 y 轴的交叉点 name =Height (cm),# 设定 y 轴标题 breaks=seq(0,50,10),# 设定 Y 轴的数据间隔 limits = c(10,50) # 设定 Y 轴的数据上下限 )+ scale_color_brewer(palette = YlOrRd)+ theme() a 出图如下:
个人分类: 软件使用|22915 次阅读|0 个评论
Before and after graphs in R
chuanpengdong 2016-8-9 15:54
A before-after graph like this library(ggplot2) df- read.table(demo.txt, header=T, stringsAsFactors=FALSE) dodge = position_dodge(width=0.2) p = ggplot(df, aes(x, y, group=Pair)) + geom_jitter(position=dodge, color=red, fill = red, size = 3) + geom_line(position=dodge, size=0.8) ############################### ############################### However you should to figure out if this is just what you want, does it be better than waterfall or vioplot? Generally, A big different in Control and Experiment suitable for this kind plot. Demofile: demo.txt
个人分类: 生物信息|11 次阅读|0 个评论
ggplot2作图实例
wuzefeng 2015-3-1 17:28
ggplot2(学R的应该都知道)越来越受到多数科研工作者的青睐,然而在作图时复杂的参数设置让初学者“忘”而却步,本文以我分析的数据为例,详细介绍一下ggplot2作图的思路: 1.数据格式(.csv,格式)#即逗号分隔的两列数据(下面只列出部分数据) 1,T.urartu 1,T.urartu 2,T.urartu 2,A.tauschii 2,A.tauschii 2,A.tauschii 2,W A-genome 2,W B-genome ........... ........... ........... 89,A.tauschii 89,A.tauschii 89,A.tauschii 90,W A-genome 90,W D-genome #这里简单介绍一下我的数据来源于含义,第一列是90个转座子family(本人研究的是小麦基因组和小麦祖先A和D基因组中的转座子元件),每个family的数据有5个可能的来源,即T.urartu、A.tauschii、W A-genome 、W B-genome 和W D-genome(做小麦的都懂得(以现有的数据来说)),因此作图的目的就是想看一下每个family里面,这五个来源的比例。 思来想去,发现ggplot2中棒状图或柱状图函数p+geom_bar()比较适合, 所以直接上代码: library(ggplot2) #调用ggplot2(没有安装的请先安装) my_data-read.csv(’fam_sub_rename.csv',head=TRUE) #读取文件 label - list(expression(italic('A.tauschii')), expression(italic('T.urartu')),W A-genome,W B-genome,W D-genome) #设置图例的内容ps: 用expression函数的目的是斜体显示。 cbbPalette - c(palegreen4, coral4, wheat3, wheat2, wheat1) # 设置填充的颜色 gg_normal - ggplot(data = my_data, aes(x = factor(family), fill = source)) #确定数据以及坐标轴 gg_normal +geom_bar(position = fill) #颜色填充 +ggtitle(Distribution) #作图的main title +ylab(expression(paste(italic(Helitron ),Ratio))) #y轴标签名称 +xlab(expression(paste(italic(Helitron ),Family))) #x轴标签名称 +theme(panel.background=element_blank(),#去掉主题(个人认为默认主题颜色不太好,去掉更简洁) panel.grid.minor=element_blank(),#网格线也去掉 text = element_text(size=10),#所有文本相关属性参数设置 axis.text.x = element_text(size=5,colour = 'black',angle=90,vjust=0.5))#设置x轴刻度文本的参数(注意与上一个区别) +scale_fill_manual(values=cbbPalette,labels = label) #填充的内容 +coord_cartesian(ylim = c(0, 1)) #坐标系范围 最后直接上图
6449 次阅读|0 个评论
USE R 分析数据的好帮手
zhuangwei 2014-8-16 10:55
最近一段时间在分析通量数据,有几个思路,蒸散的辐射限制和水分供应限制。一直在围绕着这个思路,用R不断地画图。这个过程中,看到R 统计画图的强大。一个简洁明了的图,就可以作为很好的论点的证明。特意买了一本《ggplot2 数据分析与图形艺术》 画图的书。ggplot2 对数据集有要求的:必须是一个数据框。之前对这个先入的条件不是很适应,总是先得重现构建一个新的data.frame,来满足ggplot画图的需要。看了Hakley Wichham的解释,理解了。“ggplot2对数据集的这种要求是有充分理由的。数据非常重要,我们最好明确对数据做了怎样的处理。ggplot2 会从我们给定的数据框中提取绘图所需要的变量,并生成一个新的数据集,而不是直接在数据集上进行数据变换。”这样原先的数据集就得到了保护。 关于本书详见豆瓣评价 http://book.douban.com/subject/24527091/ 附上我昨天用ggplot2 画的图、代码和所需数据。比起R 基本的画图函数plot,ggplot2 的画图语法很简洁,输出也很方便。 gpp.site.RData #以下为R code----- Sd_GPP_day_6_one_web.R # you can skip the above step and load data directly from gpp.site.RData----- load('gpp.site.RData') GPP6sub - GPP.site$data # ggplot ------- # 六个站点全部在一张图上 scatter.ggplot - ggplot(aes(x = Sd , y = GPP, colour=Season), data = GPP6sub)+ geom_point() + scale_colour_manual(values = c('grey','black')) # facet_wrap可以把六个站点分别画出来 g.site - scatter.ggplot + facet_wrap(~Site_f) + theme_bw() # geom_smooth 加入平滑线 GPP.site - g.site + geom_smooth(method='lm') # xlab, ylab 设置 GPP.site - GPP.site + xlab(expression(paste('Sd (MJ m'^'-2',' day'^'-1',')'))) + ylab(expression(paste('GPP', ' (g C m'^'-2','day'^'-1',')'))) # 把图片作为一个对象保存起来,画图所需的数据集也保存了,方便后面发表时再修改 #save(GPP.site,file='E:/data/report/gpp.site.RData') save(GPP.site,file='gpp.site.RData') pdf('Sd_GPP_6_one.pdf',width=8,height=6) print(GPP.site) dev.off()
个人分类: R|4118 次阅读|0 个评论
[转载]Plot HeatMap with R
热度 1 chuangma2006 2012-9-12 13:58
原文来自: http://www.qiuworld.com/blog/archives/2477 我们在分析了差异表达数据之后,经常要生成一种直观图--热图(heatmap)。这一节就以基因芯片数据为例,示例生成高品质的热图。比如 钢蓝渐白配色的热图 首先还是从最简单的heatmap开始。 library ( ggplot2 ) library ( ALL ) #可以使用biocLite("ALL")安装该数据包 data ( "ALL" ) library ( limma ) eset - ALL f - factor ( as. character ( eset$mol. biol ) ) design - model. matrix ( ~f ) fit - eBayes ( lmFit ( eset , design ) ) #对基因芯片数据进行分析,得到差异表达的数据 selected - p. adjust ( fit$p. value ) 0.001 esetSel - eset #选择其中一部分绘制热图 dim ( esetSel ) #从这尺度上看,数目并不多,但也不少。如果基因数过多,可以分两次做图。 Features Samples 84 47 library ( hgu95av2. db ) data - exprs ( esetSel ) probes - rownames ( data ) symbol - mget ( probes , hgu95av2SYMBOL , ifnotfound = NA ) symbol - do . call ( rbind , symbol ) symbol ) , 1 ] - rownames ( symbol ) ) ] rownames ( data ) - symbol #给每行以基因名替换探针名命名,在绘制热图时直接显示基因名。 heatmap ( data , cexRow = 0.5 ) 使用heatmap函数默认颜色生成的热图 这个图有三个部分,样品分枝树图和基因分枝树图,以及热图本身。之所以对样品进行聚类分析排序,是因为这次的样品本身并没有分组。如果有分组的话,那么可以关闭对样品的聚类分析。对基因进行聚类分析排序,主要是为了色块好看,其实可以选择不排序,或者使用GO聚类分析排序。上面的这种热图,方便简单,效果非常不错。 接下来我们假设样品是分好组的,那么我们想用不同的颜色来把样品组标记出来,那么我们可以使用ColSideColors参数来实现。同时,我们希望变更热图的渐变填充色,可以使用col参数来实现。 color. map - function ( mol. biol ) { if ( mol. biol == "ALL1/AF4" ) "#FF0000" else "#0000FF" } patientcolors - unlist ( lapply ( esetSel$mol. bio , color. map ) ) heatmap ( data , col = topo. colors ( 100 ) , ColSideColors = patientcolors , cexRow = 0.5 ) 使用heatmap函数top.colors填充生成的热图 在heatmap函数中,样品分组只能有一种,如果样品分组有多次分组怎么办?heatmap.plus就是来解决这个问题的。它们的参数都一致,除了ColSideColors和RowSideColors。heatmap使用是一维数组,而heatmap.plus使用的是字符矩阵来设置这两个参数。 library ( heatmap. plus ) hc - hclust ( dist ( t ( data ) ) ) dd. col - as. dendrogram ( hc ) groups - cutree ( hc , k = 5 ) color. map - function ( mol. biol ) { if ( mol. biol == "ALL1/AF4" ) 1 else 2 } patientcolors - unlist ( lapply ( esetSel$mol. bio , color. map ) ) col. patientcol - rbind ( groups , patientcolors ) mode ( col. patientcol ) - "character" heatmap. plus ( data , ColSideColors = t ( col. patientcol ) , cexRow = 0.5 ) 使用heatmap.plus绘制热图 这样绘图的不足是没有热图色key值。gplots中的heatmap.2为我们解决了这个问题。而且它带来了更多的预设填充色。下面就是几个例子。 library ( "gplots" ) heatmap.2 ( data , col = redgreen ( 75 ) , scale = "row" , ColSideColors = patientcolors , key = TRUE , symkey = FALSE , density. info = "none" , trace = "none" , cexRow = 0.5 ) 使用heatmap.2函数,readgreen渐变色填充生成的热图 heatmap.2 ( data , col = heat. colors ( 100 ) , scale = "row" , ColSideColors = patientcolors , key = TRUE , symkey = FALSE , density. info = "none" , trace = "none" , cexRow = 0.5 ) heatmap.2 ( data , col = terrain. colors ( 100 ) , scale = "row" , ColSideColors = patientcolors , key = TRUE , symkey = FALSE , density. info = "none" , trace = "none" , cexRow = 0.5 ) heatmap.2 ( data , col = cm. colors ( 100 ) , scale = "row" , ColSideColors = patientcolors , key = TRUE , symkey = FALSE , density. info = "none" , trace = "none" , cexRow = 0.5 ) heatmap.2 ( data , col = redblue ( 100 ) , scale = "row" , ColSideColors = patientcolors , key = TRUE , symkey = FALSE , density. info = "none" , trace = "none" , cexRow = 0.5 ) heatmap.2 ( data , col = colorpanel ( 100 , low = "white" , high = "steelblue" ) , scale = "row" , ColSideColors = patientcolors , key = TRUE , keysize = 1 , symkey = FALSE , density. info = "none" , trace = "none" , cexRow = 0.5 ) 使用heatmap.2函数,heat.colors渐变色填充生成的热图 使用heatmap.2函数,terrain.colors渐变色填充生成的热图 使用heatmap.2函数,cm.colors渐变色填充生成的热图 使用heatmap.2函数,redblue渐变色填充生成的热图 使用heatmap.2函数,colorpanel渐变色填充生成的热图 然而,以上的heatmap以及heatmap.2虽然方便简单,效果也很不错,可以使用colorpanel方便的设置渐变填充色,但是它的布局没有办法改变,生成的效果图显得有点呆板,不简洁。为此这里介绍如何使用ggplot2当中的geom_tile来为基因芯片绘制理想的热图。 library(ggplot2) hc-hclust(dist(data)) rowInd-hc$order hc-hclust(dist(t(data))) colInd-hc$order data.m-data #聚类分析的作用是为了色块集中,显示效果好。如果本身就对样品有分组,基因有排序,就可以跳过这一步。 data.m-apply(data.m,1,rescale) #以行为基准对数据进行变换,使每一行都变成[0,1]之间的数字。变换的方法可以是scale,rescale等等,按照自己的需要来变换。 data.m-t(data.m) #变换以后转置了。 coln-colnames(data.m) rown-rownames(data.m) #保存样品及基因名称。因为geom_tile会对它们按坐标重排,所以需要使用数字把它们的序列固定下来。 colnames(data.m)-1:ncol(data.m) rownames(data.m)-1:nrow(data.m) data.m-melt(data.m) #转换数据成适合geom_tile使用的形式 head(data.m) X1 X2 value 1 1 1 0.1898007 2 2 1 0.6627467 3 3 1 0.5417057 4 4 1 0.4877054 5 5 1 0.5096474 6 6 1 0.2626248 base_size-12 #设置默认字体大小,依照样品或者基因的多少而微变。 (p - ggplot(data.m, aes(X2, X1)) + geom_tile(aes(fill = value), #设定横坐标为以前的列,纵坐标为以前的行,填充色为转换后的数据 colour = "white") + scale_fill_gradient(low = "white", #设定渐变色的低值为白色,变值为钢蓝色。 high = "steelblue")) p + theme_grey(base_size = base_size) + labs(x = "", #设置xlabel及ylabel为空 y = "") + scale_x_continuous(expand = c(0, 0),labels=coln,breaks=1:length(coln)) + #设置x坐标扩展部分为0,刻度为之前的样品名 scale_y_continuous(expand = c(0, 0),labels=rown,breaks=1:length(rown)) + opts( #设置y坐标扩展部分为0,刻度为之前的基因名 axis.ticks = theme_blank(), axis.text.x = theme_text(size = base_size * #设置坐标字体为基准的0.8倍,贴近坐标对节,x坐标旋转90度,色彩为中灰 0.8, angle = 90, hjust = 0, colour = "grey50"), axis.text.y = theme_text( size = base_size * 0.8, hjust=1, colour="grey50")) 使用ggplot2中geom_tile函数,钢蓝渐白配色的热图 也可以很轻松的实现传统渐变填充色,红黄渐变。 ( p - ggplot ( data. m , aes ( X2 , X1 ) ) + geom_tile ( aes ( fill = value ) , colour = "white" ) + scale_fill_gradient ( low = "yellow" , high = "red" ) ) p + theme_grey ( base_size = base_size ) + labs ( x = "" , y = "" ) + scale_x_continuous ( expand = c ( 0 , 0 ) , labels = coln , breaks = 1 : length ( coln ) ) + scale_y_continuous ( expand = c ( 0 , 0 ) , labels = rown , breaks = 1 : length ( rown ) ) + opts ( axis. ticks = theme_blank ( ) , axis. text . x = theme_text ( size = base_size * 0.8 , angle = 90 , hjust = 0 , colour = "grey50" ) , axis. text . y = theme_text ( size = base_size * 0.8 , hjust = 1 , colour = "grey50" ) ) 使用ggplot2中geom_tile函数,红黄渐变填充的热图 使用红绿渐变填充。 ( p - ggplot ( data. m , aes ( X2 , X1 ) ) + geom_tile ( aes ( fill = value ) , colour = "white" ) + scale_fill_gradient ( low = "green" , high = "red" ) ) p + theme_grey ( base_size = base_size ) + labs ( x = "" , y = "" ) + scale_x_continuous ( expand = c ( 0 , 0 ) , labels = coln , breaks = 1 : length ( coln ) ) + scale_y_continuous ( expand = c ( 0 , 0 ) , labels = rown , breaks = 1 : length ( rown ) ) + opts ( axis. ticks = theme_blank ( ) , axis. text . x = theme_text ( size = base_size * 0.8 , angle = 90 , hjust = 0 , colour = "grey50" ) , axis. text . y = theme_text ( size = base_size * 0.8 , hjust = 1 , colour = "grey50" ) ) 使用ggplot2中geom_tile函数,红绿渐变填充的热图 使用绿白渐变填充。 ( p - ggplot ( data. m , aes ( X2 , X1 ) ) + geom_tile ( aes ( fill = value ) , colour = "white" ) + scale_fill_gradient ( low = "seagreen" , high = "white" ) ) p + theme_grey ( base_size = base_size ) + labs ( x = "" , y = "" ) + scale_x_continuous ( expand = c ( 0 , 0 ) , labels = coln , breaks = 1 : length ( coln ) ) + scale_y_continuous ( expand = c ( 0 , 0 ) , labels = rown , breaks = 1 : length ( rown ) ) + opts ( axis. ticks = theme_blank ( ) , axis. text . x = theme_text ( size = base_size * 0.8 , angle = 90 , hjust = 0 , colour = "grey50" ) , axis. text . y = theme_text ( size = base_size * 0.8 , hjust = 1 , colour = "grey50" ) ) 使用ggplot2中geom_tile函数,绿白渐变填充的热图 使用棕白渐变填充。 ( p - ggplot ( data. m , aes ( X2 , X1 ) ) + geom_tile ( aes ( fill = value ) , colour = "white" ) + scale_fill_gradient ( low = "white" , high = "sienna4" ) ) p + theme_grey ( base_size = base_size ) + labs ( x = "" , y = "" ) + scale_x_continuous ( expand = c ( 0 , 0 ) , labels = coln , breaks = 1 : length ( coln ) ) + scale_y_continuous ( expand = c ( 0 , 0 ) , labels = rown , breaks = 1 : length ( rown ) ) + opts ( axis. ticks = theme_blank ( ) , axis. text . x = theme_text ( size = base_size * 0.8 , angle = 90 , hjust = 0 , colour = "grey50" ) , axis. text . y = theme_text ( size = base_size * 0.8 , hjust = 1 , colour = "grey50" ) ) 使用ggplot2中geom_tile函数,棕白渐变填充的热图 使用灰阶填充。 ( p - ggplot ( data. m , aes ( X2 , X1 ) ) + geom_tile ( aes ( fill = value ) , colour = "white" ) + scale_fill_gradient ( low = "black" , high = "gray85" ) ) p + theme_grey ( base_size = base_size ) + labs ( x = "" , y = "" ) + scale_x_continuous ( expand = c ( 0 , 0 ) , labels = coln , breaks = 1 : length ( coln ) ) + scale_y_continuous ( expand = c ( 0 , 0 ) , labels = rown , breaks = 1 : length ( rown ) ) + opts ( axis. ticks = theme_blank ( ) , axis. text . x = theme_text ( size = base_size * 0.8 , angle = 90 , hjust = 0 , colour = "grey50" ) , axis. text . y = theme_text ( size = base_size * 0.8 , hjust = 1 , colour = "grey50" ) ) 使用ggplot2中geom_tile函数,灰色渐变填充的热图 除了ggplot2,还有lattice也是不错的选择。我只使用一种填充色,生成两个图,以作示例。 hc - hclust ( dist ( data ) ) dd. row - as. dendrogram ( hc ) row. ord - order. dendrogram ( dd. row ) #介绍另一种获得排序的办法 hc - hclust ( dist ( t ( data ) ) ) dd. col - as. dendrogram ( hc ) col. ord - order. dendrogram ( dd. col ) data. m - data library ( ggplot2 ) data. m - apply ( data. m , 1 , rescale ) #rescale是ggplot2当中的一个函数 library ( lattice ) levelplot ( data. m , aspect = "fill" , xlab = "" , ylab = "" , scales = list ( x = list ( rot = 90 , cex = 0.8 ) , y = list ( cex = 0.5 ) ) , colorkey = list ( space = "left" ) , col. regions = heat. colors ) library ( latticeExtra ) levelplot ( data. m , aspect = "fill" , xlab = "" , ylab = "" , scales = list ( x = list ( rot = 90 , cex = 0.5 ) , y = list ( cex = 0.4 ) ) , colorkey = list ( space = "left" ) , col. regions = heat. colors , legend = list ( right = list ( fun = dendrogramGrob , #dendrogramGrob是latticeExtra中绘制树型图的一个函数 args = list ( x = dd. row , ord = row. ord , side = "right" , size = 5 ) ) , top = list ( fun = dendrogramGrob , args = list ( x = dd. col , side = "top" , type = "triangle" ) ) ) ) #使用三角型构图 使用lattice中的levelplot函数,heat.colors填充绘制热图 使用lattice中的levelplot函数,heat.colors填充,dendrogramGrob绘树型,绘制热图 可是可是,绘制一个漂亮的热图这么难么?参数如此之多,设置如此复杂,色彩还需要自己指定。有没有简单到发指的函数呢?有!那就是pheatmap,全称pretty heatmaps. library ( pheatmap ) pheatmap ( data , fontsize = 9 , fontsize_row = 6 ) #最简单地直接出图 pheatmap ( data , scale = "row" , clustering_distance_row = "correlation" , fontsize = 9 , fontsize_row = 6 ) #改变排序算法 pheatmap ( data , color = colorRampPalette ( c ( "navy" , "white" , "firebrick3" ) ) ( 50 ) , fontsize = 9 , fontsize_row = 6 ) #自定义颜色 pheatmap ( data , cluster_row = FALSE , fontsize = 9 , fontsize_row = 6 ) #关闭按行排序 pheatmap ( data , legend = FALSE , fontsize = 9 , fontsize_row = 6 ) #关闭图例 pheatmap ( data , cellwidth = 6 , cellheight = 5 , fontsize = 9 , fontsize_row = 6 ) #设定格子的尺寸 color. map - function ( mol. biol ) { if ( mol. biol == "ALL1/AF4" ) 1 else 2 } patientcolors - unlist ( lapply ( esetSel$mol. bio , color. map ) ) hc - hclust ( dist ( t ( data ) ) ) dd. col - as. dendrogram ( hc ) groups - cutree ( hc , k = 7 ) annotation - data. frame ( Var1 = factor ( patientcolors , labels = c ( "class1" , "class2" ) ) , Var2 = groups ) pheatmap ( data , annotation = annotation , fontsize = 9 , fontsize_row = 6 ) #为样品分组 Var1 = c ( "navy" , "skyblue" ) Var2 = c ( "snow" , "steelblue" ) names ( Var1 ) = c ( "class1" , "class2" ) ann_colors = list ( Var1 = Var1 , Var2 = Var2 ) pheatmap ( data , annotation = annotation , annotation_colors = ann_colors , fontsize = 9 , fontsize_row = 6 ) #为分组的样品设定颜色 pheatmap最简单地直接出图 pheatmap改变排序算法 pheatmap自定义颜色 pheatmap关闭按行排序 pheatmap关闭图例 pheatmap设定格子的尺寸 pheatmap为样品分组 pheatmap为分组的样品设定颜色
个人分类: R|10571 次阅读|3 个评论

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-6-2 18:01

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部