经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » R语言 » 查看文章
R语言学习之线图的绘制详解
来源:jb51  时间:2022/3/1 17:40:31  对本文有异议

线图

线图是反映趋势变化的一种方式,其输入数据一般也是一个矩阵。

单线图

假设有这么一个矩阵,第一列为转录起始位点及其上下游5 kb的区域,第二列为H3K27ac修饰在这些区域的丰度,想绘制一张线图展示。

  1. profile="Pos;H3K27ac
  2. -5000;8.7
  3. -4000;8.4
  4. -3000;8.3
  5. -2000;7.2
  6. -1000;3.6
  7. 0;3.6
  8. 1000;7.1
  9. 2000;8.2
  10. 3000;8.4
  11. 4000;8.5
  12. 5000;8.5"

读入数据 (经过前面几篇的联系,这应该都很熟了)

  1. profile_text <- read.table(text=profile, header=T, row.names=1, quote="",sep=";")
  2. profile_text
  1. ? ? ?H3K27ac
  2. -5000 ? ? 8.7
  3. -4000 ? ? 8.4
  4. -3000 ? ? 8.3
  5. -2000 ? ? 7.2
  6. -1000 ? ? 3.6
  7. 0 ? ? ? ? 3.6
  8. 1000 ? ? ?7.1
  9. 2000 ? ? ?8.2
  10. 3000 ? ? ?8.4
  11. 4000 ? ? ?8.5
  12. 5000 ? ? ?8.5
  1. # 在melt时保留位置信息
  2. # melt格式是ggplot2画图最喜欢的格式
  3. # 好好体会下这个格式,虽然多占用了不少空间,但是确实很方便
  4.  
  5. # 这里可以用 `xvariable`,也可以是其它字符串,但需要保证后面与这里的一致
  6. # 因为这一列是要在X轴显示,所以起名为`xvariable`。
  7. profile_text$xvariable = rownames(profile_text)
  8. library(ggplot2)
  9. library(reshape2)
  10. data_m <- melt(profile_text, id.vars=c("xvariable"))
  11. data_m
  1. ? xvariable variable value
  2. 1 ? ? ?-5000 ?H3K27ac ? 8.7
  3. 2 ? ? ?-4000 ?H3K27ac ? 8.4
  4. 3 ? ? ?-3000 ?H3K27ac ? 8.3
  5. 4 ? ? ?-2000 ?H3K27ac ? 7.2
  6. 5 ? ? ?-1000 ?H3K27ac ? 3.6
  7. 6 ? ? ? ? ?0 ?H3K27ac ? 3.6
  8. 7 ? ? ? 1000 ?H3K27ac ? 7.1
  9. 8 ? ? ? 2000 ?H3K27ac ? 8.2
  10. 9 ? ? ? 3000 ?H3K27ac ? 8.4
  11. 10 ? ? ?4000 ?H3K27ac ? 8.5
  12. 11 ? ? ?5000 ?H3K27ac ? 8.5

然后开始画图,与上面画heatmap一样。

  1. # variable和value为矩阵melt后的两列的名字,内部变量, variable代表了点线的属性,value代表对应的值。
  2. p <- ggplot(data_m, aes(x=xvariable, y=value),color=variable) + geom_line()
  3. p
  4. # 图会存储在当前目录的Rplots.pdf文件中,如果用Rstudio,可以不运行dev.off()
  5. dev.off()

满心期待一个倒钟形曲线,结果,

什么也没有。

仔细看,出来一段提示

  1. geom_path: Each group consists of only one observation.
  2. Do you need to adjust the group aesthetic?

原来默认ggplot2把每个点都视作了一个分组,什么都没画出来。而data_m中的数据都来源于一个分组H3K27ac,分组的名字为variable,修改下脚本,看看效果。

  1. p <- ggplot(data_m, aes(x=xvariable, y=value,color=variable,group=variable)) +
  2. ? ? geom_line() + theme(legend.position=c(0.1,0.9))
  3. p
  4. dev.off()

图出来了,一条线,看一眼没问题;再仔细看,不对了,怎么还不是倒钟形,原来横坐标错位了。

检查下数据格式

  1. summary(data_m)
  1. ?xvariable ? ? ?variable ? ? ? ?
  2. Length:11 ? ? ? H3K27ac:11 ? ??
  3. Class :character ? ? ? ? ? ??
  4. Mode ?:character ? ? ? ?

问题来了,xvariable虽然看上去数字,但存储的实际是字符串 (因为是作为行名字读取的),需要转换为数字。

  1. data_m$xvariable <- as.numeric(data_m$xvariable)
  2.  
  3. #再检验下
  4. is.numeric(data_m$xvariable)
  1. [1] TRUE

好了,继续画图。

  1. # 注意断行时,加号在行尾,不能放在行首
  2. p <- ggplot(data_m, aes(x=xvariable, y=value,color=variable,group=variable)) +
  3. ? ? geom_line() + theme(legend.position=c(0.1,0.8))
  4. p
  5. dev.off()

图终于出来了,调了下legend的位置,看上去有点意思了。

有点难看,如果平滑下,会不会好一些,stat_smooth可以对绘制的线进行局部拟合。在不影响变化趋势的情况下,可以使用 (但慎用)。

  1. p <- ggplot(data_m, aes(x=xvariable, y=value,color=variable,group=variable)) +
  2. ? ? geom_line() + stat_smooth(method="auto", se=FALSE) +
  3. ? ? theme(legend.position=c(0.1,0.8))
  4. p
  5. dev.off()

从图中看,趋势还是一致的,线条更优美了。另外一个方式是增加区间的数量,线也会好些,而且更真实。

stat_smoothgeom_line各绘制了一条线,只保留一条就好。

  1. p <- ggplot(data_m, aes(x=xvariable, y=value,color=variable,group=variable)) +
  2. ? ? stat_smooth(method="auto", se=FALSE) + theme(legend.position=c(0.1,0.8))
  3. p
  4. dev.off()

好了,终于完成了单条线图的绘制。

多线图

那么再来一个多线图的例子吧,只要给之前的数据矩阵多加几列就好了。

  1. profile = "Pos;h3k27ac;ctcf;enhancer;h3k4me3;polII
  2. -5000;8.7;10.7;11.7;10;8.3
  3. -4000;8.4;10.8;11.8;9.8;7.8
  4. -3000;8.3;10.5;12.2;9.4;7
  5. -2000;7.2;10.9;12.7;8.4;4.8
  6. -1000;3.6;8.5;12.8;4.8;1.3
  7. 0;3.6;8.5;13.4;5.2;1.5
  8. 1000;7.1;10.9;12.4;8.1;4.9
  9. 2000;8.2;10.7;12.4;9.5;7.7
  10. 3000;8.4;10.4;12;9.8;7.9
  11. 4000;8.5;10.6;11.7;9.7;8.2
  12. 5000;8.5;10.6;11.7;10;8.2"
  13.  
  14. profile_text <- read.table(text=profile, header=T, row.names=1, quote="",sep=";")
  15.  
  16. profile_text$xvariable = rownames(profile_text)
  17. data_m <- melt(profile_text, id.vars=c("xvariable"))
  18. data_m$xvariable <- as.numeric(data_m$xvariable)
  19.  
  20. # 这里group=variable,而不是group=1 (如果上面你用的是1的话)
  21. # variable和value为矩阵melt后的两列的名字,内部变量, variable代表了点线的属性,value代表对应的值。
  22. p <- ggplot(data_m, aes(x=xvariable, y=value,color=variable,group=variable)) +
  23. ? ? stat_smooth(method="auto", se=FALSE) + theme(legend.position=c(0.85,0.2))
  24. p
  25. dev.off()

横轴文本线图

如果横轴是文本,又该怎么调整顺序呢?还记得之前热图旁的行或列的顺序调整吗?重新设置变量的factor水平就可以控制其顺序。

  1. profile = "Pos;h3k27ac;ctcf;enhancer;h3k4me3;polII
  2. -5000;8.7;10.7;11.7;10;8.3
  3. -4000;8.4;10.8;11.8;9.8;7.8
  4. -3000;8.3;10.5;12.2;9.4;7
  5. -2000;7.2;10.9;12.7;8.4;4.8
  6. -1000;3.6;8.5;12.8;4.8;1.3
  7. 0;3.6;8.5;13.4;5.2;1.5
  8. 1000;7.1;10.9;12.4;8.1;4.9
  9. 2000;8.2;10.7;12.4;9.5;7.7
  10. 3000;8.4;10.4;12;9.8;7.9
  11. 4000;8.5;10.6;11.7;9.7;8.2
  12. 5000;8.5;10.6;11.7;10;8.2"
  13.  
  14. profile_text <- read.table(text=profile, header=T, row.names=1, quote="",sep=";")
  15.  
  16. profile_text_rownames <- row.names(profile_text)
  17.  
  18. profile_text$xvariable = rownames(profile_text)
  19. data_m <- melt(profile_text, id.vars=c("xvariable"))
  20.  
  21. # 就是这一句,会经常用到
  22. data_m$xvariable <- factor(data_m$xvariable, levels=profile_text_rownames, ordered=T)
  23.  
  24. # geom_line设置线的粗细和透明度
  25. p <- ggplot(data_m, aes(x=xvariable, y=value,color=variable,group=variable)) +
  26. ? ? geom_line(size=1, alpha=0.9) + theme(legend.position=c(0.85,0.2)) +
  27. ? ? theme(axis.text.x=element_text(angle=45,hjust=1, vjust=1))
  28.  
  29. # stat_smooth
  30. #p <- ggplot(data_m, aes(x=xvariable, y=value,color=variable,group=variable)) +
  31. # ? ? stat_smooth(method="auto", se=FALSE) + theme(legend.position=c(0.85,0.2)) +
  32. # ? ? theme(axis.text.x=element_text(angle=45,hjust=1, vjust=1))
  33.  
  34. p
  35. dev.off()

比较下位置信息做为数字(前面的线图)和位置信息横轴的差别。当为数值时,ggplot2会选择合适的几个刻度做标记,当为文本时,会全部标记。另外文本横轴,smooth效果不明显 (下面第2张图)。

至此完成了线图的基本绘制,虽然还可以,但还有不少需要提高的地方,比如在线图上加一条或几条垂线、加个水平线、修改X轴的标记(比如0换为TSS)、设置每条线的颜色等。具体且听下回一步线图法。

到此这篇关于R语言学习之线图的绘制详解的文章就介绍到这了,更多相关R语言绘制线图内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号