背景
熟悉ggplot2绘图,有一本书,可以介绍大家使用,《R数据可视化手册》第二版
代码语言:javascript复制https://www.bookdown.org/
可以在上述网址中找到网页版本。
书中的例子代码:
代码语言:javascript复制library(gcookbook)
uspop
colnames(uspopage)
ggplot(data = uspopage,mapping = aes(x=Year,y=Thousands,fill =AgeGroup))
geom_area()
一、散点图
代码语言:javascript复制x <- read.table("prok_representative.csv",sep = ",",header = T);
head(x)
ggplot(data = x,aes(x=Size,y=Genes)) geom_point()
ggplot(data = x,aes(x=Size,y=Genes)) geom_point(size=1,color="blue")
fit <- lm(data = x,Genes~ Size)
summary(fit)
fit
ggplot(data = x,aes(x=Size,y=Genes)) geom_point(size=1,color="blue")
geom_abline(intercept = 286.6,slope = 843.7,col="red",lwd=1)
p <- ggplot(data = x,aes(x=Size,y=Genes)) geom_point(size=1,color="blue") geom_abline(intercept = 286.6,slope = 843.7,col="red",lwd=1)
p annotate(geom = "text",x=4,y=10000,label="y=286x 843.7nR2=0.9676")
p annotate(geom = "text",x=4,y=10000,label="y=286x 843.7nR2=0.9676")
labs(title="Genome Size vs Gene Number",x="Genome Size",y="Genes")
ggplot2 绘制基因组大小与基因数目相关性图
二、直方图
代码语言:javascript复制x <- read.table("H37Rv.gff",sep = "t",header = F,skip = 7,quote = "")
x <- x[x$V3=="gene",]
x <- abs(x$V5-x$V4 1)
length(x)
range(x)
ggplot(data = NULL,aes(x=x))
ggplot(data = NULL,aes(x=x)) geom_histogram(bins = 80)
ggplot(data = NULL,aes(x=x)) geom_histogram(bins = 80) geom_rug()
# library(dplyr)
# x <- read.table("H37Rv.gff",sep = "t",header = F,skip = 7,quote = "")
# x %>% dplyr::filter(V3 == 'gene') %>% dplyr::mutate(gene_len = abs(V5-V4) 1)%>% ggplot(aes(x=gene_len)) geom_histogram(bins=80)
# x %>% dplyr::filter(V3 == 'gene') %>% dplyr::mutate(gene_len = abs(V5-V4) 1)%>% ggplot(aes(x=gene_len)) geom_histogram(bins=80,fill='cyan',color='black') geom_rug() theme_light() labs(title='Histogram')
ggplot2 绘制基因长度分布直方图
三、条形图
代码语言:javascript复制# hg19_len <- read.csv(file = "homo_length.csv",header = T)
# x <- hg19_len[1:24,]
# head(x)
# ggplot(data = x,aes(x=chr,y=length,fill=chr)) geom_bar(stat = "identity")
# p <- ggplot(data = x,aes(x=chr,y=length,fill=chr)) geom_bar(stat = "identity")
# p scale_x_discrete(limits=x$chr)
# p scale_x_discrete(limits=x$chr) coord_flip()
# p scale_x_discrete(limits=x$chr) coord_flip() guides(fill=FALSE)
x <- read.csv(file = "homo_length.csv",header = T)
x %>% dplyr::slice(1:24) %>% ggplot(aes(x=chr,y=length)) geom_bar(stat = 'identity') scale_x_discrete(limits=x$chr[1:24])
x %>% dplyr::slice(1:24) %>% ggplot(aes(x=chr,y=length,fill=chr)) geom_bar(stat = 'identity') scale_x_discrete(limits=x$chr[1:24]) scale_fill_manual(values=rainbow(24))
x %>% dplyr::slice(1:24) %>% ggplot(aes(x=chr,y=length)) geom_bar(stat = 'identity') scale_x_discrete(limits=x$chr[1:24])
coord_flip()
x %>% dplyr::slice(1:24) %>% ggplot(aes(x=chr,y=length,fill=chr)) geom_bar(stat = 'identity') scale_x_discrete(limits=x$chr[1:24])
coord_flip() scale_fill_manual(values = c(rep('red',24)))
library(RColorBrewer)
x %>% dplyr::slice(1:24) %>% ggplot(aes(x=chr,y=length,fill=chr)) geom_bar(stat = 'identity') scale_x_discrete(limits=x$chr[1:24])
coord_flip() scale_fill_manual(values = c(rep(brewer.pal(4,'Set1'),6)))
x %>% dplyr::slice(1:24) %>% ggplot(aes(x=chr,y=length,fill=chr)) geom_bar(stat = 'identity') scale_x_discrete(limits=x$chr[1:24])
coord_flip() scale_fill_manual(values = c(rep(brewer.pal(4,'Set1'),6)))
guides(fill='none')
ggplot2 绘制人染色体长度分布图
写在最后:有时间我们会努力更新的。大家互动交流可以前去论坛,地址在下面,复制去浏览器即可访问,弥补下公众号没有留言功能的缺憾。
代码语言:javascript复制bioinfoer.com
有些板块也可以预设为大家日常趣事的分享等,欢迎大家来提建议。