生信喵 发表于 2022-9-27 10:56:11

ggplot2散点图直方图条形图

背景
       熟悉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 <- 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   
# 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)
x %>% dplyr::slice(1:24) %>% ggplot(aes(x=chr,y=length,fill=chr)) + geom_bar(stat = 'identity')+ scale_x_discrete(limits=x$chr) + 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) +
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) +
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) +
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) +
coord_flip() + scale_fill_manual(values = c(rep(brewer.pal(4,'Set1'),6))) +
guides(fill='none')      
       ggplot2 绘制人染色体长度分布图
页: [1]
查看完整版本: ggplot2散点图直方图条形图