直播!R语言入门和ggplot2科研数据可视化入门分享!就在今晚,欢迎大家参加呀!
会议时间:2021/03/17 19:30-21:30 (GMT+08:00)
点击链接入会,或添加至会议列表:https://meeting.tencent.com/s/H9goXk0EWnqO
会议 ID:714 933 296
手机一键拨号入会+8675536550000,,714933296# (中国大陆)+85230018898,,,2,714933296# (中国香港)
根据您的位置拨号+8675536550000 (中国大陆)+85230018898 (中国香港)
以下是我这次分享的内容
代码和数据在公众号后台回复 20210317 直接获得
R语言和Rstudio以及ggplot2的安装可以参考
B站 徐洲更 视频 可能是最好的R语言安装指南
B站 小明的数据分析笔记本 视频 R语言/RStudio/ggplot2的安装过程
绘图的第一步就是准备数据
我自己的习惯是使用excel准备数据,然后另存为csv格式的文件,最后使用R语言的read.csv()函数读入
getwd()
'D:/Jupyter/ggplot2_introduction'
library(ggplot2)
函数控制整体,参数调节细节
绘图固定的写法是
ggplot(data=df,aes(x=Var1,y=Var2))
接下来就是想要做什么图,对应叠加对应的函数就好了
比如对应的散点图
ggplot(data=df,aes(x=Var1,y=Var2))+geom_point()
柱形图
ggplot(data=df,aes(x=Var1,y=Var2))+geom_col()
1、散点图
df1<-read.csv('example_data/scatter_plot_example.csv',header=T)
head(df1)
ggplot(data=df1,aes(x=var1,y=var2))+
geom_point()
ggplot(data=df1,aes(x=var1,y=var2))+geom_point(color='red',size=10,shape=18,alpha=0.5)
按照分类变量填充颜色、形状
ggplot(data=df1,aes(x=var1,y=var2,color=var3,shape=var3))+geom_point(size=5)
自定义颜色形状等
ggplot(data=df1,aes(x=var1,y=var2,color=var3,shape=var3))+geom_point(size=5)+scale_color_manual(values=c('steelblue', 'yellowgreen', 'violetred1'))
ggplot(data=df1,aes(x=var1,y=var2,color=var3,shape=var3))+
geom_point(size=5)+
scale_color_manual(values=c('steelblue', 'yellowgreen', 'violetred1'))+
scale_shape_manual(values=c(9,10,11))
2、气泡图
df2<-read.csv('example_data/bubble_plot_example.csv',header=T)head(df2)
ggplot(data=df2,aes(x=var1,y=var2,color=var3,size=var4))+
geom_point(alpha=0.8)+
scale_size_continuous(range=c(1,50))+
theme_bw()+
theme(legend.position='none')+
ylim(0,0.4)+
scale_color_manual(values=c('steelblue', 'yellowgreen', 'violetred1'))
3、折线图
df3<-read.csv('example_data/line_example.csv',header=T)head(df3)
ggplot(data=df3,aes(x=time_point,y=value))+
geom_line()+
geom_point()+
geom_errorbar(aes(ymin=value-sd,ymax=value+sd),width=0.15)+
theme_bw()
4、柱形图
df4<-read.csv('example_data/bar_plot_example.csv',header=T)head(df4)
ggplot(data=df4,aes(x=var1,y=var2))+
geom_col()
ggplot(data=df4,aes(x=var1,y=var2))+geom_col(color='red',fill='lightblue')
ggplot(data=df4,aes(x=var1,y=var2))+
geom_col(aes(fill=var1))+
theme_bw()+
scale_fill_manual(values = c('#008fd5','#ff2700','#77ab43','#ffd700'))+
scale_y_continuous(expand = c(0,0),limits = c(0,8))
df5<-read.csv('example_data/bar_plot_example_1.csv',header=T)head(df5)
ggplot(data=df5,aes(x=var3,y=var2,fill=var1))+
geom_bar(stat='identity')+
theme_bw()+
scale_fill_manual(values=c('steelblue', 'yellowgreen', 'violetred1'))+
scale_y_continuous(expand = c(0,0),limits = c(0,20))
ggplot(data=df5,aes(x=var3,y=var2,fill=var1))+geom_bar(stat='identity')+theme_bw()+scale_fill_manual(values=c('steelblue', 'yellowgreen', 'violetred1'))+scale_y_continuous(expand = c(0,0),limits = c(0,20))
ggplot(data=df5,aes(x=var3,y=var2,fill=var1))+
geom_bar(stat='identity',position='dodge')+
scale_fill_manual(values=c('steelblue', 'yellowgreen', 'violetred1'))+
scale_y_continuous(expand = c(0,0),limits = c(0,10))+
theme_bw()
5、箱线图
write.csv(ToothGrowth,file='example_data/ToothGrowth.csv',quote=F,row.names=F)
df6<-read.csv('example_data/ToothGrowth.csv',header=T)
head(df6)
table(df6$dose)
df6$dose<-factor(df6$dose,levels = c('0.5','1','2'))
ggplot(data=df6,aes(x=dose,y=len,fill=dose))+
geom_boxplot()+
scale_fill_manual(values = c('steelblue', 'yellowgreen', 'violetred1'))+
theme_bw()+
stat_boxplot(geom = 'errorbar', aes(ymin = ..ymax..),width=0.2)+
stat_boxplot(geom = 'errorbar', aes(ymax = ..ymin..),width=0.2)
6、小提琴图
ggplot(data=df6,aes(x=dose,y=len,fill=dose))+geom_violin()+theme_bw()+scale_fill_manual(values=c('steelblue', 'yellowgreen', 'violetred1'))
ggplot(data=df6,aes(x=dose,y=len,fill=dose))+
geom_violin()+
geom_boxplot(width=0.3)+
scale_fill_manual(values = c('steelblue', 'yellowgreen', 'violetred1'))+
theme_bw()+
stat_boxplot(geom = 'errorbar', aes(ymin = ..ymax..),width=0.2)+
stat_boxplot(geom = 'errorbar', aes(ymax = ..ymin..),width=0.2)
7、热图
df7<-read.csv('example_data/pheatmap_example_data.csv',header=T)
head(df7)
library(reshape2)
df7.1<-melt(df7,id.vars = 'gene_name')
head(df7.1)
ggplot(data=df7.1,aes(x=variable,y=gene_name,fill=value))+
geom_tile(color='black')+
theme(axis.text.x = element_text(angle=90,hjust=0.5,vjust=0.5),axis.ticks = element_blank())+
scale_fill_gradient(low = 'steelblue',high = 'violetred1')+
labs(x=NULL,y=NULL)+
geom_text(aes(label=round(value,1)))
library(patchwork)
p1<-ggplot(data=df2,aes(x=var1,y=var2,color=var3,size=var4))+
geom_point(alpha=0.8)+
scale_size_continuous(range=c(1,50))+
theme_bw()+
theme(legend.position='none')+
ylim(0,0.4)+
scale_color_manual(values=c('steelblue', 'yellowgreen', 'violetred1'))
p2<-ggplot(data=df4,aes(x=var1,y=var2))+geom_col(aes(fill=var1))+theme_bw()+scale_fill_manual(values = c('#008fd5','#ff2700','#77ab43','#ffd700'))+scale_y_continuous(expand = c(0,0),limits = c(0,8))
p3<-ggplot(data=df5,aes(x=var3,y=var2,fill=var1))+
geom_bar(stat='identity')+
theme_bw()+
scale_fill_manual(values=c('steelblue', 'yellowgreen', 'violetred1'))+
scale_y_continuous(expand = c(0,0),limits = c(0,20))
p4<-ggplot(data=df6,aes(x=dose,y=len,fill=dose))+geom_violin()+geom_boxplot(width=0.3)+scale_fill_manual(values = c('steelblue', 'yellowgreen', 'violetred1'))+theme_bw()+stat_boxplot(geom = 'errorbar', aes(ymin = ..ymax..),width=0.2)+stat_boxplot(geom = 'errorbar', aes(ymax = ..ymin..),width=0.2)
p1+p2+p3+p4+plot_layout(guides='collect',ncol=2,byrow = T)+plot_annotation(tag_levels = 'A')
pp<-p1+p2+p3+p4+plot_layout(guides='collect',ncol=2)
ggsave(filename = 'example_data/result_plot.pdf',
pp,
width=10,
heigh=10)
ggsave(filename = 'example_data/result_plot.tiff', pp, width=10, heigh=10, dpi=300)
欢迎大家关注我的公众号 小明的数据分析笔记本
小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!
赞 (0)