Lesson 06 for Plotting in R for Biologists
作者简介
taoyan:R语言中文社区特约作家,伪码农,R语言爱好者,爱开源。
个人博客: https://ytlogos.github.io/
前文传送门:
R语言可视化学习笔记之相关矩阵可视化包ggcorrplot
R语言学习笔记之相关性矩阵分析及其可视化
ggplot2学习笔记系列之利用ggplot2绘制误差棒及显著性标记
ggplot2学习笔记系列之主题(theme)设置
用circlize包绘制circos-plot
利用gganimate可视化R-Ladies发展情况
一篇关于国旗与奥运会奖牌的可视化笔记
利用ggseqlogo绘制seqlogo图
R语言data manipulation学习笔记之创建变量、重命名、数据融合
R语言data manipulation学习笔记之subset data
R语言可视化学习笔记之gganimate包
创建属于自己的调色板
Lesson 01 for Plotting in R for Biologists
Lesson 02&03 for Plotting in R for Biologists
Lesson 04 for Plotting in R for Biologists
Lesson 05 for Plotting in R for Biologists
前面几节课绘制的图形只有条形图,但是我们科研过程中需要绘制各种各样的图形来展现我们的数据,这节课就主要讲一下利用ggplot2绘制各种图形,这也是ggplot2的魔力所在。相同的数据可以通过不同类型的图形来可视化。本节课主要将绘制以下几类图形:
条形图
直方图
散点图
箱线图
小提琴图
密度图
点状图
线图
饼图
韦恩图
数据加载清洗
library(tidyverse)
theme_set(theme_gray(base_size = 18))
my_data <- read.csv("variants_from_assembly.bed", sep = "\t", quote = '', stringsAsFactors = FALSE)
names(my_data) <- c("chrom","start","stop","name","size","strand","type","ref.dist","query.dist")
head(my_data)
## chrom start stop name size strand type ref.dist query.dist
## 1 6 102958468 102958469 SV2 317 + Insertion -14 303
## 2 6 102741692 102741693 SV3 130 + Deletion 130 0
## 3 6 102283759 102283760 SV4 1271 + Insertion -12 1259
## 4 6 101194032 101194033 SV5 2864 + Insertion -13 2851
## 5 6 101056644 101056645 SV6 265 + Insertion 0 265
## 6 6 100407499 100407500 SV7 334 + Insertion 0 334
summary(my_data$chrom)
## Length Class Mode
## 9555 character character
#数据过滤
my_data <- my_data[my_data$chrom %in% c(seq(1:22), "X", "Y","MT"), ]
#染色体排序
my_data$chrom <- factor(my_data$chrom, levels = c(seq(1:22), "X", "Y","MT"))
#类型(type)排序
my_data$type <- factor(my_data$type, levels = c("Insertion","Deletion","Expansion","Contraction"))
head(my_data)
## chrom start stop name size strand type ref.dist query.dist
## 1 6 102958468 102958469 SV2 317 + Insertion -14 303
## 2 6 102741692 102741693 SV3 130 + Deletion 130 0
## 3 6 102283759 102283760 SV4 1271 + Insertion -12 1259
## 4 6 101194032 101194033 SV5 2864 + Insertion -13 2851
## 5 6 101056644 101056645 SV6 265 + Insertion 0 265
## 6 6 100407499 100407500 SV7 334 + Insertion 0 334
可视化
条形图
ggplot(my_data, aes(x=chrom, fill=type))+geom_bar()
直方图
ggplot(my_data, aes(x=size, fill=type))+geom_histogram()
ggplot(my_data, aes(x=size, fill=type))+geom_histogram()+xlim(0,500)
ggplot(my_data, aes(x=size, fill=type))+geom_histogram(binwidth = 5)+xlim(0,500)
散点图
#将type映射给颜色
ggplot(my_data, aes(x=ref.dist, y=query.dist, color=type))+geom_point()
ggplot(my_data, aes(x=ref.dist, y=query.dist, color=type))+geom_point()+xlim(-500,500)+ylim(-500,500)
#将数值型变量size映射给颜色
ggplot(my_data, aes(x=ref.dist, y=query.dist, color=size))+geom_point()+xlim(-500,500)+ylim(-500,500)
ggplot(my_data, aes(x=ref.dist, y=query.dist,color=size))+geom_point()+xlim(-500,500)+ylim(-500,500)+scale_color_gradient(limits=c(0, 500))
箱线图
ggplot(my_data, aes(type, y=size))+geom_boxplot()
ggplot(my_data, aes(x=type, y=size, fill=type))+geom_boxplot()
ggplot(my_data, aes(x=type, y=size, fill=type))+geom_boxplot()+coord_flip()
小提琴图
ggplot(my_data, aes(x=type, y=size, fill=type))+geom_violin()+ylim(0,1000)+guides(fill=FALSE)
ggplot(my_data, aes(x=type, y=size, fill=type))+geom_violin(adjust=0.2)+ylim(0,1000)+guides(fill=FALSE)#adjust调整分辨率,默认为1,值越低,分辨率越高
#坐标变换
ggplot(my_data, aes(x=type, y=size, fill=type))+geom_violin()+scale_y_log10()
密度图
ggplot(my_data, aes(x=size, fill=type))+geom_density()+xlim(0,500)
ggplot(my_data, aes(x=size, fill=type))+geom_density(position = "stack")+xlim(0,500)
ggplot(my_data, aes(x=size, fill=type))+geom_density(alpha=0.5)+xlim(0,500)
#图形分面
ggplot(my_data, aes(x=size, fill=type))+geom_density()+xlim(0,500)+facet_grid(type~.)
点状图
ggplot(my_data, aes(x=size, fill=type))+geom_dotplot()
线图
time_cource <- read.csv("time_course_data.txt", sep = ",", quote = '', stringsAsFactors = TRUE, header = TRUE)
head(time_cource)
## seconds value sample
## 1 0 0.00 A
## 2 1 5.97 A
## 3 2 13.42 A
## 4 3 56.08 A
## 5 4 98.04 A
## 6 5 27.11 A
ggplot(time_cource, aes(x=seconds, y=value, color=sample))+geom_line()
ggplot(time_cource, aes(x=seconds, y=value, color=sample))+geom_line(size=3)
饼图
type_counts <- summary(my_data$type)
type_counts
## Insertion Deletion Expansion Contraction
## 3141 2256 2476 1674
library(RColorBrewer)
pie(type_counts, col = brewer.pal(length(type_counts), "Set1"))
韦恩图
这里利用四个基因集
listA <- read.csv("genes_list_A.txt", header = FALSE)
A <- listA$V1
listB <- read.csv("genes_list_B.txt", header = FALSE)
B <- listB$V1
listC <- read.csv("genes_list_C.txt", header = FALSE)
C <- listC$V1
listD <- read.csv("genes_list_D.txt", header = FALSE)
D <- listD$V1
library(VennDiagram)
#注意这个包绘制的图只能直接保存在文件中,无法实时显示
venn.diagram(list(A=A, B=B, C=C, D=D), fill=c("yellow","red","cyan","forestgreen"), cex=1.5, filename = "Venn_diagram_genes_4.png")
SessionInfo
sessionInfo()
## R version 3.4.3 (2017-11-30)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 16299)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936
## [2] LC_CTYPE=Chinese (Simplified)_China.936
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C
## [5] LC_TIME=Chinese (Simplified)_China.936
##
## attached base packages:
## [1] grid stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] VennDiagram_1.6.18 futile.logger_1.4.3 RColorBrewer_1.1-2
## [4] forcats_0.2.0 stringr_1.2.0 dplyr_0.7.4
## [7] purrr_0.2.4 readr_1.1.1 tidyr_0.7.2
## [10] tibble_1.4.2 ggplot2_2.2.1.9000 tidyverse_1.2.1
##
## loaded via a namespace (and not attached):
## [1] reshape2_1.4.3 haven_1.1.1 lattice_0.20-35
## [4] colorspace_1.3-2 htmltools_0.3.6 yaml_2.1.16
## [7] rlang_0.1.6 pillar_1.1.0 foreign_0.8-69
## [10] glue_1.2.0 lambda.r_1.2 modelr_0.1.1
## [13] readxl_1.0.0 bindrcpp_0.2 bindr_0.1
## [16] plyr_1.8.4 munsell_0.4.3 gtable_0.2.0
## [19] cellranger_1.1.0 rvest_0.3.2 psych_1.7.8
## [22] evaluate_0.10.1 labeling_0.3 knitr_1.18
## [25] parallel_3.4.3 broom_0.4.3 Rcpp_0.12.15
## [28] scales_0.5.0.9000 backports_1.1.2 jsonlite_1.5
## [31] mnormt_1.5-5 hms_0.4.1 digest_0.6.14
## [34] stringi_1.1.6 rprojroot_1.3-2 cli_1.0.0
## [37] tools_3.4.3 magrittr_1.5 lazyeval_0.2.1
## [40] futile.options_1.0.0 crayon_1.3.4 pkgconfig_2.0.1
## [43] xml2_1.2.0 lubridate_1.7.1 assertthat_0.2.0
## [46] rmarkdown_1.8 httr_1.3.1 rstudioapi_0.7
## [49] R6_2.2.2 nlme_3.1-131 compiler_3.4.3
往期精彩内容整理合集
2017年R语言发展报告(国内)
R语言中文社区历史文章整理(作者篇)
R语言中文社区历史文章整理(类型篇)
公众号后台回复关键字即可学习
回复 R R语言快速入门及数据挖掘
回复 Kaggle案例 Kaggle十大案例精讲(连载中)
回复 文本挖掘 手把手教你做文本挖掘
回复 可视化 R语言可视化在商务场景中的应用
回复 大数据 大数据系列免费视频教程
回复 量化投资 张丹教你如何用R语言量化投资
回复 用户画像 京东大数据,揭秘用户画像
回复 数据挖掘 常用数据挖掘算法原理解释与应用
回复 机器学习 人工智能系列之机器学习与实践
回复 爬虫 R语言爬虫实战案例分享
Lesson 06 for Plotting in R for Biologists相关推荐
- 备份恢复Lesson 06.Performing Backups
备份恢复Lesson 06.Performing Backups 1.对于Oracle建议的备份恢复策略,默认恢复时间最长是多少,给出详细解释. 6-6 2.请说明快速增量备份的实现原理? 6-7 4 ...
- [Java in NetBeans] Lesson 06. Custom classes
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Constructors: A special method called when an object of the class ...
- Java学习lesson 06
匿名对象 * 节省代码,并且很多情况下是针对某个方法只需要调用一次的情况下. * 匿名对象可以调用方法,也可以给成员变量赋值. 同时也可以作为方法的参数进行传递. public class AnonD ...
- 20170408 mofan pandas:code lesson 06
import pandas as pd import numpy as np import os print(os.getcwd())print('**生成DataFrame************* ...
- 数据分析-R语言资料整理
独家分享--48页PPT解密数据可视化! Excel图表快捷操作小技巧 基于随机森林的分类与回归 R语言制作网页 ggplot2:可视化设计师的神器,了解一下 [译]R包介绍:Online Rando ...
- 精心整理 | R语言中文社区历史文章整理(类型篇)
2018年过去一半了~又到了盘点的时间~感谢长时间来各位好友的关注,我们的成长与你们的爱护是分不开的.更感谢各位老师的投稿,支撑起了我们的这个社区,让更多R语言的爱好者和从业者获得最棒的知识!本文选取 ...
- [小北De编程手记] : Lesson 02 - Selenium For C# 之 核心对象
从这一篇开始,开始正式的介绍Selenium 以及相关的组件,本文的将讨论如下问题: Selenium基本的概念以及在企业化测试框架中的位置 Selenium核心对象(浏览器驱动) Web Drive ...
- 第八届中国R语言会议(北京)纪要
本文转载至:http://cos.name/2015/06/8th_china_r_beijing/ 第八届中国R语言会议(北京会场)暨2015北大光华数据与价值论坛于2015年6月6日至7日在北京大 ...
- R从文件中读取数据,输出文件
看了几天的书,终于到这一步了,说实话,用R来做统计,很少有人手动的去输入那些数字,肯定是从别的地方导入的,我们用来处理就可以了,所以到这里才算是真正的入门,前面都是做基础的练手. 我学习R从<R ...
- R语言-散布图—plot的函数使用
plot()的使用 注意:用到的数据(自行下载): 首先,这个就是先help一下,这是新手必须要学会的 会产生一以下代码帮助: | Generic X-Y Plotting Description G ...
最新文章
- Buffer和Cache的区别
- 教你 4 步搭建弹性可扩展的 WebAPI
- 小米手机qq不读取相册照片_小米手机越用越卡怎么办?
- 【Redis】Redis 替代方案
- HDU 2604 Queuing(矩阵高速幂)
- 【专题三】如何考量虚拟化的投资回报率?——服务器虚拟化的阴暗面
- php定时任务引入文件,php解决crontab定时任务不能写入文件问题的方法分析
- Docker 外部访问容器Pp、数据管理volume、网络network 介绍
- linux抓包出现nbns,如何用抓包分析工具定位感染主机和用户
- 关于学习scala中lazy关键字的记录
- JavaScript实战操作(DOM,BOM)
- 图片·21312312
- Akka(6): become/unbecome:运算行为切换
- 论文“Structure-from-Motion Revisited” 对ISFM改进的理解
- [PWN][基础篇]基础理论
- 初级程序员最应避免的 7 大错误
- 搭建基于企业微信的SCRM系统
- 低速 CAN 总线偶尔无法正常通信,该如何诊断?
- mysql定时备份数据库-linux
- 怎么将webm格式转换成mp4,3招轻松学