生信喵 发表于 2022-9-27 23:57:56

ggplot2分组条形图以及箱线图

一、分组条形图
x <- read.csv("sv_distrubution.csv",header = T)
x
# svs <- x %>% tidyr::pivot_longer(cols = 2:5,names_to = 'variation')
svs <- x %>% gather(key = Variation,value =Number,-X)
ggplot(data = svs,aes(x=X,y=Number))+geom_bar(stat = "identity")
ggplot(data = svs,aes(x=X,y=Number,fill=Variation))+geom_bar(stat = "identity")

p <- ggplot(data = svs,aes(x=X,y=Number,fill=Variation))+geom_bar(stat = "identity")
p + scale_x_discrete(limits=x$X)
p + scale_x_discrete(limits=x$X) + scale_fill_brewer(palette = 1,type = 'seq')
p + scale_x_discrete(limits=x$X) + scale_fill_brewer(palette = 2,type = 'seq')
p + scale_x_discrete(limits=x$X) + scale_fill_brewer(palette = 2,type = 'div')
p + scale_x_discrete(limits=x$X) + scale_fill_brewer(palette = 3,type = 'div')
p + scale_x_discrete(limits=x$X) + scale_fill_brewer(palette = 4,type = 'div')
p + scale_x_discrete(limits=x$X) + scale_fill_brewer(palette = 'Set2',type = 'div')
p + scale_x_discrete(limits=x$X) + scale_fill_brewer(palette = 'Set1',type = 'div')
p + scale_x_discrete(limits=x$X) + scale_fill_brewer(palette = 'Set1')
p + scale_x_discrete(limits=x$X) + scale_fill_brewer(palette = 'Blues')

ggplot(data = svs,aes(x=X,y=Number,fill=Variation))+geom_bar(stat = "identity",position='dodge')
ggplot(data = svs,aes(x=X,y=Number,fill=Variation))+geom_bar(stat = "identity",position='dodge2')
ggplot(data = svs,aes(x=X,y=Number,fill=Variation))+geom_bar(stat = "identity",position='fill')
ggplot(data = svs,aes(x=X,y=Number,fill=Variation))+geom_bar(stat = "identity",position='stack')

p + scale_x_discrete(limits=x$X) + scale_fill_brewer(palette = 'Set1')+
labs(title ="SV Distribution",x="Chromosome Number",y="SV Numbers") +
theme(legend.position = 'bottom',plot.title = element_text(hjust = 0.5))ggplot2 绘制基因组 SV 突变堆叠条形图
p + scale_x_discrete(limits=x$X) + scale_fill_brewer(palette = 'Set1')+
labs(title ="SV Distribution",x="Chromosome Number",y="SV Numbers") +
theme(legend.position = 'bottom',plot.title = element_text(hjust = 0.5)) +
coord_polar() +guides(fill='none')

二、饼图
m <- read.table("Species.txt",sep = '\t',header = FALSE)
head(m)
sum(m$V3)
name <- paste(m[,1],m[,2],'\n',m$V3,'%')
name
ggplot(data = m,aes(x = "",y=V3,fill=name))+geom_bar(stat = 'identity') +
coord_polar(theta = 'y')+guides(fill = 'none')+scale_fill_brewer(palette = 'Set1')
# y <- paste(m[,1],m[,2])
# x <- data.frame(name=y,values=m$V3/sum(m$V3))
# p <- ggplot(data = x,aes(x = "",y=values,fill=name))+geom_bar(stat = "identity",width = 1)+guides(fill='none')
# p
# p+coord_polar(theta = 'y')+labs(x = '', y = '', title = '')
ggplot2 绘制饼图

三、箱线图
head(ToothGrowth)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
#按提供药物种类分组
ggplot(data = ToothGrowth,aes(x=supp,y=len,fill=supp))+geom_boxplot()
#按剂量分组
ggplot(data = ToothGrowth,aes(x=dose,y=len,group=dose,fill=dose))+geom_boxplot()
#两组
ggplot(data = ToothGrowth,aes(x=dose,y=len,group=supp:dose,fill=supp:dose))+geom_boxplot()
ggplot(data = ToothGrowth,aes(x=dose,y=len,group=supp:dose,fill=supp))+geom_boxplot()
ggplot(data = ToothGrowth,aes(x=dose,y=len,group=supp:dose,fill=dose))+geom_boxplot()
ggplot(data = ToothGrowth,aes(x=supp,y=len,group=supp:dose,fill=dose))+geom_boxplot()

#box图加抖动点
p<- ggplot(data = ToothGrowth,aes(x=dose,y=len,fill=dose))+geom_boxplot()+
geom_jitter(width = 0.1)
p+labs(title = 'ToothGrowth VC vs OJ',x='DOSE',y='Length')+
scale_fill_brewer(palette = 'Set1') +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position = 'bottom')
ggplot2 绘制箱线图加抖动的点
#分面
ggplot(data = ToothGrowth,aes(x=supp,y=len,group=supp:dose,fill=supp))+geom_boxplot()+
scale_fill_brewer(palette = 'Set1')+
facet_grid(~ supp,scales = 'free')

ggplot2 绘制分面箱线图

页: [1]
查看完整版本: ggplot2分组条形图以及箱线图