DAY6 生信入門-R語言作圖

圖就是數(shù)據(jù),數(shù)據(jù)就是圖

常見可視化R包

1. 作圖

  • base 略顯陳舊 了解一下
  • ggplot2 中堅(jiān)力量 學(xué)起來有點(diǎn)難
  • ggpubr 江湖救急 ggplot2簡(jiǎn)化和美化 褒貶不一

2. 拼圖

  • par里的mfrow:給基礎(chǔ)包畫出的圖拼圖。mfrow:基礎(chǔ)包拼圖最好用的函數(shù)。
#基礎(chǔ)包拼圖
#基礎(chǔ)包畫出的圖無法賦值
par(mfrow=c(1,2))
plot(1:100)
plot(100:1)

基礎(chǔ)包-繪圖函數(shù)

image.png

高級(jí) 低級(jí)繪圖函數(shù)區(qū)別:能不能單獨(dú)出一張圖


image.png
plot(iris[,1],iris[,3],col = iris[,5]) 
text(6.5,4, labels = 'hello')
boxplot(iris[,1]~iris[,5])
dev.off()

ggplot2 語法

  1. 入門級(jí)繪圖模板
  2. 映射-顏色、大小、透明度、形狀
  3. 分面
  4. 幾何對(duì)象
  5. 統(tǒng)計(jì)變換
  6. 位置調(diào)整
  7. 坐標(biāo)系

1. 入門級(jí)模版

ggplot(data = <DATA>)+
<GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))

ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length)) #注意沒有引號(hào)
image.png

2. 映射:按照數(shù)據(jù)框的某一列來定義圖的某個(gè)屬性

映射是專屬ggplot的名詞,表跟數(shù)據(jù)有關(guān)的屬性設(shè)置


image
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))
#默認(rèn)顏色,紅綠藍(lán)
image.png

手動(dòng)設(shè)置

與映射區(qū)分,跟數(shù)據(jù)本身無關(guān)


image.png
ggplot(data = mpg) +
geom_point(mapping = aes( x= displ, y= hwy), color = "blue")   

映射vs手動(dòng)設(shè)置

image.png

映射實(shí)際參數(shù)是列名且不加引號(hào)

3. 分面

把一張圖變成多個(gè)子圖

ggplot(data = iris) + 
  geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + 
  facet_wrap(~ Species) 
# facet_wrap(~ Species) :根據(jù)某一列按縱坐標(biāo)方向把一張大圖分成n張小圖,n和這一列有多少個(gè)取值有關(guān)系。
#這一列的要求:1.有重復(fù)值 2.取值數(shù)量有限
image.png

3. 雙分面

test = iris
test$group = sample(letters[1:5],150,replace = T)#新增一列
ggplot(data = test) + 
  geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + 
  facet_grid(group ~ Species)

image.png

image.png

這些點(diǎn)滿足的條件:group =a,Species=setosa

練習(xí)題

#練習(xí)6-1
# 示例數(shù)據(jù):ggplot2中數(shù)據(jù)集mpg
# 1.分別以mpg的displ和hwy兩列作為橫縱坐標(biāo),畫點(diǎn)圖。
ggplot(mpg) +
  geom_point(aes(x = displ, y = hwy))
> mpg #查看mpg數(shù)據(jù)類型tibble,優(yōu)化版的data frame,顯示行列數(shù),每列數(shù)據(jù)類型
# A tibble: 234 x 11
   manufacturer model   displ  year   cyl trans  drv     cty   hwy fl    class
   <chr>        <chr>   <dbl> <int> <int> <chr>  <chr> <int> <int> <chr> <chr>
 1 audi         a4        1.8  1999     4 auto(~ f        18    29 p     comp~
 2 audi         a4        1.8  1999     4 manua~ f        21    29 p     comp~
 3 audi         a4        2    2008     4 manua~ f        20    31 p     comp~
 4 audi         a4        2    2008     4 auto(~ f        21    30 p     comp~
 5 audi         a4        2.8  1999     6 auto(~ f        16    26 p     comp~
 6 audi         a4        2.8  1999     6 manua~ f        18    26 p     comp~
 7 audi         a4        3.1  2008     6 auto(~ f        18    27 p     comp~
 8 audi         a4 qua~   1.8  1999     4 manua~ 4        18    26 p     comp~
 9 audi         a4 qua~   1.8  1999     4 auto(~ 4        16    25 p     comp~
10 audi         a4 qua~   2    2008     4 manua~ 4        20    28 p     comp~
# ... with 224 more rows
# 2.嘗試修改顏色或大小,從mpg數(shù)據(jù)框中任選可以用來分類的列。
ggplot(data = mpg)+
  geom_point(mapping = aes(x =displ,
                           y = hwy,
                           color=class))
#hwy 是數(shù)值列,連續(xù)型數(shù)據(jù),顏色漸變。顏色越淺數(shù)值越大。
#class 取值獨(dú)立,離散型數(shù)據(jù),獨(dú)立顏色。
image.png

image.png
# 3.根據(jù)class列來分面
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy)) + 
  facet_wrap(~ class,ncol = 4) 
#和nrow = 2一樣
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy)) + 
  facet_grid(class ~ .)
#按縱向分圖
image.png

image.png
# 4.根據(jù)drv和cyl兩個(gè)變量來分面
ggplot(mpg)+
  geom_point(aes(x= displ, y = hwy))+
  facet_grid(drv~cyl)
#facet_grid沒有nrow,ncol選項(xiàng),永遠(yuǎn)方方正正

4. 幾何對(duì)象

image

理解分組

ggplot(data = test) + 
  geom_smooth(mapping = aes(x = Sepal.Length, 
                          y = Petal.Length))

image.png

顯式分組
一條趨勢(shì)線分為三條趨勢(shì)線

ggplot(data = test) + 
  geom_smooth(mapping = aes(x = Sepal.Length, 
                            y = Petal.Length,
                            group = Species)) 

image.png

隱式分組
顏色+分組,一條線分為三條線

ggplot(data = test) + 
  geom_smooth(mapping = aes(x = Sepal.Length, 
                          y = Petal.Length,
                          color = Species))  ### 會(huì)畫出三條線

image.png

幾何對(duì)象可以疊加

一個(gè)geom函數(shù)畫出的所有東西就是一個(gè)幾何對(duì)象

ggplot(data = test) + 
  geom_smooth(mapping = aes(x = Sepal.Length, 
                          y = Petal.Length))+
  geom_point(mapping = aes(x = Sepal.Length, 
                           y = Petal.Length)) #局部映射,只管當(dāng)前函數(shù)畫出的幾何對(duì)象

ggplot(data = test,mapping = aes(x = Sepal.Length, y = Petal.Length))+
  geom_smooth()+ #括號(hào)要寫,空著
  geom_point() #全局映射,少幾次復(fù)制粘貼

image.png

映射分局部映射和全局映射
局部映射僅對(duì)當(dāng)前圖層有效,全局映射對(duì)所有圖層有效
圖層:geom_xx()畫出的單個(gè)幾何對(duì)象

練習(xí)題

# 1.嘗試寫出下圖的代碼
# 數(shù)據(jù)是iris
# X軸是Species
# y軸是Sepal.Width
# 圖是箱線圖
ggplot(data = iris,mapping = aes(x = Species,y = Sepal.Width,color = Species))+
  geom_boxplot()
#魔鬼操作:color,fill都設(shè)置,箱線的線消失,失去中位數(shù)
image.png
# 2\. 嘗試在此圖上疊加點(diǎn)圖,
# 能發(fā)現(xiàn)什么問題?
ggplot(data = iris,mapping = aes(x = Species,y = Sepal.Width,color = Species))+
  geom_boxplot()+
  geom_point()
#圖上點(diǎn)的數(shù)量不夠50,點(diǎn)重合,無法分辨
#先寫的圖層被壓在了下面
image.png
# 3.用下列代碼作圖,觀察結(jié)果
ggplot(test,aes(x = Sepal.Length,y = Petal.Length,color = Species)) +
  geom_point()+
  geom_smooth(color = "black")
# 請(qǐng)問,當(dāng)局部映射和全局映射沖突,以誰為準(zhǔn)?
#局部映射
#smooth不能以連續(xù)型數(shù)據(jù)定義顏色
image.png
練習(xí)題反饋出來的問題
  • 當(dāng)局部映射和全局映射沖突,以局部映射為準(zhǔn)
  • 圖層疊加與覆蓋的問題
  • 點(diǎn)圖重疊的問題
    <meta charset="utf-8">

5. 統(tǒng)計(jì)變換

library(ggplot2)
ggplot(data = diamonds) + #原始數(shù)據(jù)
  geom_bar(mapping = aes(x = cut)) #y是自己數(shù)的
#幾何對(duì)象函數(shù)
image
ggplot(data = diamonds) + 
  stat_count(mapping = aes(x = cut))
#統(tǒng)計(jì)函數(shù)
image

統(tǒng)計(jì)變換使用場(chǎng)景

1. 不統(tǒng)計(jì),數(shù)據(jù)直接做圖

fre = as.data.frame(table(diamonds$cut))

ggplot(data = fre) +
  geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity")#加參,給出橫縱坐標(biāo),不統(tǒng)計(jì)

image.png
image

2.count改為prop

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))

image
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, y = ..prop..)) 
#不加group = 1,出問題,柱形高度全部相等
image.png

6.位置關(guān)系

抖動(dòng)的點(diǎn)圖

ggplot(data = mpg,mapping = aes(x = class, 
                                y = hwy,
                                group = class)) + 
  geom_boxplot()+
  geom_point()

不對(duì)
ggplot(data = mpg,mapping = aes(x = class, 
                                y = hwy,
                                group = class)) + 
  geom_boxplot()+
  geom_jitter()
#正確的,橫坐標(biāo)位置范圍擴(kuò)大
# 注意:這里是換函數(shù),把geom_point換成geom_jitter,而不是加函數(shù)
正確的箱線圖上疊加點(diǎn)圖

堆疊直方圖 默認(rèn)

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut,fill=clarity))

image

并列直方圖

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
#加position參數(shù)
image

7. 坐標(biāo)系

1. 翻轉(zhuǎn)坐標(biāo)系 coord_flip()
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + 
  geom_boxplot() +
  coord_flip()
#注意:這里是加函數(shù)
image
2. 極坐標(biāo)系coord_polar()
bar <- ggplot(data = diamonds) + 
  geom_bar(
    mapping = aes(x = cut, fill = cut), 
    show.legend = FALSE,
    width = 1
  ) + 
  theme(aspect.ratio = 1) +
  labs(x = NULL, y = NULL)
bar + coord_flip()

image
bar + coord_polar()

image

完整繪圖模板

圖層之間可以使用不同數(shù)據(jù)


image.png

練習(xí)6-3

test=iris
ggplot(data = test, mapping = aes(x = Species, y = Sepal.Width)) + 
 geom_violin(aes(fill=Species))+
 geom_boxplot() +
 geom_jitter(mapping = aes(shape=Species))+  
 coord_flip()
ggsave("practice6-3.png") 

image

ggpubr

ggpubr 搜代碼直接用,基本不需要系統(tǒng)學(xué)習(xí)
sthda上有大量ggpubr出的圖

library(ggpubr)
ggscatter(iris,x="Sepal.Length",y="Petal.Length",color="Species")
#區(qū)別:ggscatter相當(dāng)于ggplot+geom_;加引號(hào);圖例正上方;去掉了灰色背景
#ggplot加參數(shù)也可以達(dá)到一樣的效果
ggplot(iris,aes(x = Sepal.Length,y = Petal.Length,color = Species)) +
  geom_point()+
  theme_classic()+
  theme(legend.position = "top")
image
p <- ggboxplot(iris, x = "Species", y = "Sepal.Length",
          color = "Species", shape = "Species",
          add = "jitter")
p
#賦值,給圖命名一般用P1,P2
image
my_comparisons <- list( c("setosa", "versicolor"), c("setosa", "virginica"), c("versicolor", "virginica") )
#組成的是:長(zhǎng)度為2的向量組成的列表
p + stat_compare_means(comparisons = my_comparisons)+ # 組間比較,最不可替換
  stat_compare_means(label.y = 9) 
#可以用+添加,和ggplot一樣
#多句代碼用+連接,表示在畫同一張圖
#控制臺(tái)開頭的+,表示一行代碼未完
image

圖片保存

1. ggplot系列圖(包括ggpubr)通用的簡(jiǎn)便保存 ggsave

ggsave

ggsave("iris_box_ggpubr.png")

#前提:畫板清空;p已賦值
ggsave(p,filename = "iris_box_ggpubr2.png")
#在不顯示圖的基礎(chǔ)上,保存圖

2. 通用:三段論

先新建PDF,再一步步畫圖


image.png
#通用保存
pdf("iris_box_ggpubr.pdf")
boxplot(iris[,1]~iris[,5])
text(6.5,4, labels = 'hello')
dev.off()

3. eoffice包 導(dǎo)出為ppt,全部元素都是可編輯模式

library(eoffice)
topptx(p,"iris_box_ggpubr.pptx") #注意寫x
#出問題的話,就換rio包里的export,導(dǎo)出為PPT
#PPT取消組合,就變?yōu)槎鄠€(gè)可移動(dòng)的對(duì)象

拼圖

image.png
library(patchwork)
x=ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + 
  geom_boxplot() +
  coord_flip()
p1=bar + coord_flip()
p2=bar + coord_polar()
p1+p2+bar+x
image.png
p1+p2+bar+x & theme_bw() #取消灰色背景,&應(yīng)用所有圖形
image.png
p1+p2+bar+x + plot_annotation(tag_levels = "A") #給圖片加ABCD
image.png
#收集圖例,其他拼圖R包都做不了
p1+p2+bar+x + plot_layout(guides="collect")
#一樣的圖例就整理成一個(gè)
image.png
練習(xí)題 6-4
# 2\. 嘗試使用labs()
ggplot(mtcars, aes(mpg, wt, colour = cyl)) + 
  geom_point()+
  labs(title = "This is a title",subtitle = "This is a subtitle",caption = "This is a caption")+ #設(shè)置圖片標(biāo)題、子標(biāo)題、引用
  labs(x = "New x label")+
  labs(y = "New y label")+ #修改x軸y軸標(biāo)題
  labs(title = "title", tag = "A")+ #加tag
  labs(title = NULL)+
  labs(tag = NULL) #取消用NULL
image.png
# 3\. 如何在保存的同時(shí)調(diào)整比例
ggsave("x.png",width = 10,height = 10) #用width,height調(diào)整比例
image.png

代碼可運(yùn)行卻不出圖-因?yàn)楫嫲灞徽加?/h2>
  1. dev.off(), 表示關(guān)閉畫板。多次運(yùn)行,到null device 為止。再運(yùn)行出圖代碼。


    image.png

2.還不行,dev.new(),新建一個(gè)畫板,運(yùn)行代碼

  1. 還不行,重啟session, Rstudio,電腦

分享一個(gè)做美圖的網(wǎng)站

http://www.sthda.com/english/

畫圖合輯

http://www.itdecent.cn/nb/35523479

image.png

美圖代碼+你的數(shù)據(jù)+你解決問題的能力=你的圖
難點(diǎn)不是作圖代碼,而是如何將你的數(shù)據(jù)整理成示例數(shù)據(jù)的樣子

作者:Ruizheng
鏈接:http://www.itdecent.cn/p/a3fc06784f6c
來源:簡(jiǎn)書
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。
.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容