生信人

找回密码
立即注册
搜索
热搜: 活动 交友 discuz
发新帖

0

收听

12

听众

318

主题
发表于 2022-9-27 10:56:11 | 查看: 1130| 回复: 0
背景
       熟悉ggplot2绘图,有一本书,可以介绍大家使用,《R数据可视化手册》第二版
  1. https://www.bookdown.org/
复制代码
      可以在上述网址中找到网页版本。
       书中的例子代码:
  1. library(gcookbook)
  2. uspop
  3. colnames(uspopage)
  4. ggplot(data = uspopage,mapping = aes(x=Year,y=Thousands,fill =AgeGroup)) +
  5.   geom_area()
复制代码


一、散点图
  1. x <- read.table("prok_representative.csv",sep = ",",header = T);  
  2. head(x)
  3. ggplot(data = x,aes(x=Size,y=Genes))+geom_point()
  4. ggplot(data = x,aes(x=Size,y=Genes))+geom_point(size=1,color="blue")
  5. fit <- lm(data = x,Genes~ Size)
  6. summary(fit)
  7. fit
  8. ggplot(data = x,aes(x=Size,y=Genes))+geom_point(size=1,color="blue")+
  9.   geom_abline(intercept = 286.6,slope = 843.7,col="red",lwd=1)
  10. 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)
  11. p+annotate(geom = "text",x=4,y=10000,label="y=286x+843.7\nR2=0.9676")
  12. p+annotate(geom = "text",x=4,y=10000,label="y=286x+843.7\nR2=0.9676")+
  13.   labs(title="Genome Size vs Gene Number",x="Genome Size",y="Genes")
复制代码
      
       ggplot2 绘制基因组大小与基因数目相关性图


二、直方图
  1. x <- read.table("H37Rv.gff",sep = "\t",header = F,skip = 7,quote = "")  
  2. x <- x[x$V3=="gene",]  
  3. x <- abs(x$V5-x$V4+1)  
  4. length(x)  
  5. range(x)  
  6. ggplot(data = NULL,aes(x=x))
  7. ggplot(data = NULL,aes(x=x))+geom_histogram(bins = 80)
  8. ggplot(data = NULL,aes(x=x))+geom_histogram(bins = 80)+geom_rug()
  9. # library(dplyr)
  10. # x <- read.table("H37Rv.gff",sep = "\t",header = F,skip = 7,quote = "")
  11. # x %>% dplyr::filter(V3 == 'gene') %>% dplyr::mutate(gene_len = abs(V5-V4)+1)%>% ggplot(aes(x=gene_len))+geom_histogram(bins=80)
  12. # 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 绘制基因长度分布直方图

三、条形图
  1. # hg19_len <- read.csv(file = "homo_length.csv",header = T)
  2. # x <- hg19_len[1:24,]   
  3. # head(x)
  4. # ggplot(data = x,aes(x=chr,y=length,fill=chr))+geom_bar(stat = "identity")
  5. # p <- ggplot(data = x,aes(x=chr,y=length,fill=chr))+geom_bar(stat = "identity")
  6. # p+scale_x_discrete(limits=x$chr)
  7. # p+scale_x_discrete(limits=x$chr)+coord_flip()
  8. # p+scale_x_discrete(limits=x$chr)+coord_flip()+guides(fill=FALSE)

  9. x <- read.csv(file = "homo_length.csv",header = T)
  10. x %>% dplyr::slice(1:24) %>% ggplot(aes(x=chr,y=length)) + geom_bar(stat = 'identity')+ scale_x_discrete(limits=x$chr[1:24])
  11. 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))
  12. x %>% dplyr::slice(1:24) %>% ggplot(aes(x=chr,y=length)) + geom_bar(stat = 'identity')+ scale_x_discrete(limits=x$chr[1:24]) +
  13.   coord_flip()
  14. 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]) +
  15.   coord_flip() + scale_fill_manual(values = c(rep('red',24)))
  16. library(RColorBrewer)
  17. 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]) +
  18.   coord_flip() + scale_fill_manual(values = c(rep(brewer.pal(4,'Set1'),6)))
  19. 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]) +
  20.   coord_flip() + scale_fill_manual(values = c(rep(brewer.pal(4,'Set1'),6))) +
  21.   guides(fill='none')
复制代码
      
       ggplot2 绘制人染色体长度分布图

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

您需要登录后才可以回帖 登录 | 立即注册

QQ|Archiver|手机版|小黑屋|生信人 ( 萌ICP备20244422号 )

GMT+8, 2024-11-23 21:41 , Processed in 0.083734 second(s), 30 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表