R:作圖

常用可視化R包

image.png

*加粗為重點(diǎn)掌握

1. 作圖

1.1 基礎(chǔ)包:略顯陳舊,了解一下

(1) 繪圖函數(shù)

高級(jí)繪圖函數(shù)
plot() #散點(diǎn)圖等多種圖形,根據(jù)數(shù)據(jù)分類(lèi),調(diào)用相應(yīng)函數(shù)繪圖
hist() #頻率直方圖
boxplot() #箱線(xiàn)圖
stripchart() #點(diǎn)圖
barplot() #柱狀圖
dotplot() #餅圖
piechart()  #點(diǎn)圖
matplot() #數(shù)學(xué)圖形
低級(jí)繪圖函數(shù)
lines() #添加線(xiàn)
curve() #添加曲線(xiàn)
abline() #添加給定斜率的線(xiàn)
points() #添加點(diǎn)
segments() #折線(xiàn)
arrows() #箭頭
axis() #坐標(biāo)軸
box() #外框
title() #標(biāo)題
text() #文字
mtext() #圖邊文字
繪圖參數(shù)
image.png

(2)舉例

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

同樣數(shù)據(jù),ggplot、ggpubr輸出區(qū)別


image.png

image.png
dev.off() #關(guān)閉畫(huà)板

1.2 ggplot2:中堅(jiān)力量,學(xué)起來(lái)有點(diǎn)難

(1)入門(mén)級(jí)繪圖模板:作圖數(shù)據(jù),橫縱坐標(biāo)

ggplot(data = <DATA>)+
  <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
#模板
ggplot(data = test)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length))

(2)屬性設(shè)置(顏色、大小、透明度、點(diǎn)的形狀,線(xiàn)型等)

  • color(外框顏色)&fill(填充顏色)
    R色卡:https://evvail.com/2020/02/01/477.html
    RColorBrewer:http://www.itdecent.cn/p/194765c8c17e
    可以輸入顏色名稱(chēng)、16色、字符串(顏色名稱(chēng)or16進(jìn)制顏色代碼需加引號(hào),按列映射直接輸入列名可不加引號(hào))
  • size:自動(dòng)單位mm
  • alpha:透明度,0.5代表50%
  • shape:形狀


    image.png
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy), 
             color = "blue") #手動(dòng)設(shè)置,要加引號(hào)
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy), 
             size = 5,     # 點(diǎn)的大小5mm,單位自帶mm
             alpha = 0.5,  # 透明度 50%
             shape = 8)  # 點(diǎn)的形狀,自帶體系

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

  • 映射:領(lǐng)導(dǎo)思維,只說(shuō)按照某列分配顏色,不必說(shuō)具體是哪幾種顏色
  • 手動(dòng)設(shè)置:把所有的點(diǎn)設(shè)置為同一個(gè)顏色,直接指定是哪個(gè)顏色
ggplot(data = test)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species)) #aes參數(shù),列名,顏色自動(dòng)設(shè)置,不加引號(hào)
aes_string(x = ,y = )
#字符轉(zhuǎn)變?yōu)橛成?
自行指定映射的具體顏色
ggplot(data = test)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))+ 
  scale_color_manual(values = c("blue","grey","red"))
區(qū)分color和fill兩個(gè)屬性
  • 空心形狀和實(shí)心形狀都用color設(shè)置顏色
  • 既有邊框又有內(nèi)心的,才需要color和fill兩個(gè)參數(shù)
  • color管邊框,fill管實(shí)心

1.4 分面

單分面
ggplot(data = test) + 
  geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + 
  facet_wrap(~ Species)  #用species來(lái)分面
image.png
雙分面
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) #用species和group來(lái)分圖
image.png
循環(huán)中存儲(chǔ)多個(gè)圖形
  • 可以建立列表,將圖形存儲(chǔ)在列表里
  • 再用patchwork中wrap_plots進(jìn)行圖片分割
1.5 幾何對(duì)象
局部設(shè)置和全局設(shè)置
#局部設(shè)置
ggplot(data = test) + 
  geom_smooth(mapping = aes(x = Sepal.Length, 
                          y = Petal.Length))+
  geom_point(mapping = aes(x = Sepal.Length, 
                           y = Petal.Length))
#全局設(shè)置
ggplot(data = test,mapping = aes(x = Sepal.Length, y = Petal.Length))+
  geom_smooth()+
  geom_point()
  • 局部映射:僅對(duì)當(dāng)前圖層有效,單獨(dú)函數(shù)后aes()
  • 全局映射:對(duì)所有圖層有效
  • 當(dāng)局部設(shè)置和全局設(shè)置沖突,按局部設(shè)置結(jié)果運(yùn)行

2.6 統(tǒng)計(jì)變換-直方圖

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut))
ggplot(data = diamonds) + 
  stat_count(mapping = aes(x = cut))
image.png
  • geom_開(kāi)頭的函數(shù)是幾何對(duì)象函數(shù)
  • stat_開(kāi)頭函數(shù)是統(tǒng)計(jì)函數(shù)
不統(tǒng)計(jì),數(shù)據(jù)直接做圖
ggplot(data = fre) +
  geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity")
#只有寫(xiě)stat=identity才會(huì)不統(tǒng)計(jì)
不統(tǒng)計(jì)count,統(tǒng)計(jì)prop
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
#group=1是把所有對(duì)象作為一個(gè)組
image.png

1.7 位置關(guān)系

抖動(dòng)的點(diǎn)圖
ggplot(data = mpg,mapping = aes(x = class, 
                                y = hwy,
                                group = class)) + 
  geom_boxplot()+
  geom_point()
image.png
ggplot(data = mpg,mapping = aes(x = class, 
                                y = hwy,
                                group = class)) + 
  geom_boxplot()+
  geom_jitter()
#jitter可以讓點(diǎn)不重合
image.png
ggplot(data = mpg,mapping = aes(x = class, 
                                y = hwy,
                                group = class)) + 
  geom_boxplot()+
  geom_dotplot(binaxis = "y",binwidth = .5,stackdir = "center")
#dotplot散點(diǎn)很整齊,點(diǎn)不重合,也不奔放
image.png
直方圖
  • 疊放直方圖
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut,fill=clarity))
image.png
  • 并列直方圖
    position = "dodge"
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
image.png

1.8 坐標(biāo)系

  • 翻轉(zhuǎn)坐標(biāo)系,類(lèi)似轉(zhuǎn)置:coord_flip()
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + 
  geom_boxplot() +
  coord_flip()
image.png
  • 極坐標(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()
bar + coord_polar()
image.png

完整繪圖模板

ggplot(data = <DATA>)+
  <geom_function>( #繪圖函數(shù)
    mapping = aes(<MAPPINGS>), #x、y坐標(biāo)
    stat = <STAT> #直方圖中若需手動(dòng)y坐標(biāo)
    position = <POSITION>
  )+
  <COORDINATE_FUNCTION>+ #極坐標(biāo)
  <FACET_FUNCTION> #單分面、雙分面

1.3 ggpubr:江湖救急,ggplot2簡(jiǎn)化和美化,褒貶不一

  • ggpubr 搜代碼直接用,基本不需要系統(tǒng)學(xué)習(xí)
  • 基礎(chǔ)包不可以賦值,ggpubr圖可以賦值
  • sthda有大量ggpubr出的圖:www.sthda.com
  • 不可替代功能:comparisons(比較兩組之間)
p <- ggboxplot(iris, x = "Species", 
               y = "Sepal.Length",
               color = "Species", 
               shape = "Species",
               add = "jitter")
p
my_comparisons <- list( c("setosa", "versicolor"), 
                        c("setosa", "virginica"), 
                        c("versicolor", "virginica") )
p + stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value
  stat_compare_means(label.y = 9) 
image.png

圖片的保存和導(dǎo)出

1.基礎(chǔ)包

pdf("iris_box_ggpubr.pdf")
#保存格式及文件名格式一一對(duì)應(yīng)

2.ggplot系列圖(包括ggpubr)

  • ggsave()
  • 通用三段論:保存的格式及文件名,作圖代碼,關(guān)閉畫(huà)板
p <- ggboxplot(iris, x = "Species", 
               y = "Sepal.Length",
               color = "Species", 
               shape = "Species",
               add = "jitter")
ggsave(p,filename = "iris_box_ggpubr.png")
#ggplot2可以賦值

3.eoffice包

  • 導(dǎo)出為ppt,全部元素都是可編輯模式
topptx(p,"iris_box_ggpubr.pptx")

拼圖

大佬包patchwork
https://mp.weixin.qq.com/s/p7LLLvzR5LPgHhuRGhYQBQ

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 復(fù)習(xí): 文件讀取 認(rèn)識(shí)csv格式 1.直接打開(kāi):切記要顯示“.csv”后綴 記事本打開(kāi)image.png subl...
    喬幫主_d2ac閱讀 649評(píng)論 0 2
  • 圖就是數(shù)據(jù),數(shù)據(jù)就是圖 常見(jiàn)可視化R包 1. 作圖 base 略顯陳舊 了解一下 ggplot2 中堅(jiān)力量 學(xué)起來(lái)...
    Tina_e4a6閱讀 1,506評(píng)論 0 8
  • 1.常用可視化R包 作圖baseggplot2ggpubr 拼圖par里mfrowgrid.arragecowpl...
    不到7不改名閱讀 737評(píng)論 0 3
  • 常用繪圖R包 1.基礎(chǔ)包 略顯陳舊 了解一下 2.ggplot2 中堅(jiān)力量 學(xué)起來(lái)有點(diǎn)難 3.ggpubr 江湖救...
    CyberPlanet閱讀 424評(píng)論 0 3
  • 作者:嚴(yán)濤浙江大學(xué)作物遺傳育種在讀研究生(生物信息學(xué)方向)偽碼農(nóng),R語(yǔ)言愛(ài)好者,愛(ài)開(kāi)源 ggplot2學(xué)習(xí)筆記之圖...
    Dylan的迪閱讀 2,874評(píng)論 0 6

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