|
发表于 2022-9-27 10:56:11
|
查看: 1130 |
回复: 0
背景
熟悉ggplot2绘图,有一本书,可以介绍大家使用,《R数据可视化手册》第二版
- https://www.bookdown.org/
复制代码 可以在上述网址中找到网页版本。
书中的例子代码:
- library(gcookbook)
- uspop
- colnames(uspopage)
- ggplot(data = uspopage,mapping = aes(x=Year,y=Thousands,fill =AgeGroup)) +
- geom_area()
复制代码
一、散点图
- 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.7\nR2=0.9676")
- p+annotate(geom = "text",x=4,y=10000,label="y=286x+843.7\nR2=0.9676")+
- labs(title="Genome Size vs Gene Number",x="Genome Size",y="Genes")
复制代码
ggplot2 绘制基因组大小与基因数目相关性图
二、直方图
- 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 绘制基因长度分布直方图
三、条形图
- # 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 绘制人染色体长度分布图
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
|