日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

直播!R語(yǔ)言入門(mén)和ggplot2科研數(shù)據(jù)可視化入門(mén)分享!就在今晚,歡迎大家參加呀!

 weipijieli 2021-04-11

會(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.png
getwd()

'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.png
ggplot(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.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'))+
scale_shape_manual(values=c(9,10,11))
圖片
output_19_0.png

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'))
圖片
output_22_0.png

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()
圖片
output_25_0.png

4、柱形圖

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.png
ggplot(data=df4,aes(x=var1,y=var2))+
geom_col(color='red',fill='lightblue')
圖片
output_29_0.png
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))
圖片
output_30_0.png
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))
圖片
output_32_0.png
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_34_0.png
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()
圖片
output_35_0.png

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)
圖片
output_40_0.png

6、小提琴圖

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.png
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)
圖片
output_43_0.png

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)))
圖片
output_50_0.png
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')
圖片
output_56_0.png
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)

歡迎大家關(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

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多