前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ggplot2分组条形图饼图箱线图

ggplot2分组条形图饼图箱线图

作者头像
生信喵实验柴
发布2022-10-25 19:58:35
7200
发布2022-10-25 19:58:35
举报
文章被收录于专栏:生信喵实验柴

一、分组条形图

代码语言:javascript
复制
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 突变堆叠条形图

代码语言:javascript
复制
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')

二、饼图

代码语言:javascript
复制
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 绘制饼图

三、箱线图

代码语言:javascript
复制
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 绘制箱线图加抖动的点

代码语言:javascript
复制
#分面
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 绘制分面箱线图

写在最后:有时间我们会努力更新的。大家互动交流可以前去论坛,地址在下面,复制去浏览器即可访问,弥补下公众号没有留言功能的缺憾。

代码语言:javascript
复制
bioinfoer.com

有些板块也可以预设为大家日常趣事的分享等,欢迎大家来提建议。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-09-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生信喵实验柴 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档