會(huì)議時(shí)間:2021/03/17 19:30-21:30 (GMT+08:00)
點(diǎn)擊鏈接入會(huì),或添加至?xí)h列表:https://meeting.tencent.com/s/H9goXk0EWnqO
會(huì)議 ID:714 933 296
手機(jī)一鍵撥號(hào)入會(huì)+8675536550000,,714933296# (中國(guó)大陸)+85230018898,,,2,714933296# (中國(guó)香港)
根據(jù)您的位置撥號(hào)+8675536550000 (中國(guó)大陸)+85230018898 (中國(guó)香港)
以下是我這次分享的內(nèi)容
代碼和數(shù)據(jù)在公眾號(hào)后臺(tái)回復(fù) 20210317 直接獲得
R語(yǔ)言和Rstudio以及ggplot2的安裝可以參考
B站 徐洲更 視頻 可能是最好的R語(yǔ)言安裝指南
B站 小明的數(shù)據(jù)分析筆記本 視頻 R語(yǔ)言/RStudio/ggplot2的安裝過(guò)程
繪圖的第一步就是準(zhǔn)備數(shù)據(jù)
我自己的習(xí)慣是使用excel準(zhǔn)備數(shù)據(jù),然后另存為csv格式的文件,最后使用R語(yǔ)言的read.csv()函數(shù)讀入
image.png
image.pnggetwd()
'D:/Jupyter/ggplot2_introduction'
library(ggplot2)
函數(shù)控制整體,參數(shù)調(diào)節(jié)細(xì)節(jié)
繪圖固定的寫(xiě)法是
ggplot(data=df,aes(x=Var1,y=Var2))
接下來(lái)就是想要做什么圖,對(duì)應(yīng)疊加對(duì)應(yīng)的函數(shù)就好了
比如對(duì)應(yīng)的散點(diǎn)圖
ggplot(data=df,aes(x=Var1,y=Var2))+geom_point()
柱形圖
ggplot(data=df,aes(x=Var1,y=Var2))+geom_col()
1、散點(diǎn)圖
df1<-read.csv('example_data/scatter_plot_example.csv',header=T)
head(df1)
ggplot(data=df1,aes(x=var1,y=var2))+
geom_point()
output_13_0.pngggplot(data=df1,aes(x=var1,y=var2))+geom_point(color='red',size=10,shape=18,alpha=0.5)
output_14_0.png按照分類(lèi)變量填充顏色、形狀
ggplot(data=df1,aes(x=var1,y=var2,color=var3,shape=var3))+geom_point(size=5)
output_16_0.png自定義顏色形狀等
ggplot(data=df1,aes(x=var1,y=var2,color=var3,shape=var3))+
geom_point(size=5)+
scale_color_manual(values=c('steelblue', 'yellowgreen', 'violetred1'))
output_18_0.pngggplot(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))
output_19_0.png2、氣泡圖
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'))
output_22_0.png3、折線圖
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()
output_25_0.png4、柱形圖
df4<-read.csv('example_data/bar_plot_example.csv',header=T)
head(df4)
ggplot(data=df4,aes(x=var1,y=var2))+
geom_col()
output_28_0.pngggplot(data=df4,aes(x=var1,y=var2))+
geom_col(color='red',fill='lightblue')
output_29_0.pngggplot(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))
output_30_0.pngdf5<-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))
output_32_0.pngggplot(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))
output_34_0.pngggplot(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()
output_35_0.png5、箱線圖
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)
output_40_0.png6、小提琴圖
ggplot(data=df6,aes(x=dose,y=len,fill=dose))+
geom_violin()+
theme_bw()+
scale_fill_manual(values=c('steelblue', 'yellowgreen', 'violetred1'))
output_42_0.pngggplot(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)
output_43_0.png7、熱圖
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)))
output_50_0.pnglibrary(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')
output_56_0.pngpp<-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)
歡迎大家關(guān)注我的公眾號(hào) 小明的數(shù)據(jù)分析筆記本
小明的數(shù)據(jù)分析筆記本 公眾號(hào) 主要分享:1、R語(yǔ)言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡(jiǎn)單小例子;2、園藝植物相關(guān)轉(zhuǎn)錄組學(xué)、基因組學(xué)、群體遺傳學(xué)文獻(xiàn)閱讀筆記;3、生物信息學(xué)入門(mén)學(xué)習(xí)資料及自己的學(xué)習(xí)筆記!
公眾號(hào)二維碼.jpg