最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

手把手教學(xué)-使用ggpubr進行文章的組圖合并?。?!

2023-11-07 10:48 作者:爾云間  | 我要投稿

小伙伴在閱讀生信文章的時候,肯定都見過許多精美的圖片,而他們的圖片中往往都是有許多個不同結(jié)果的圖進行組合,這種是發(fā)表文獻(xiàn)是,小伙伴必須需要學(xué)會的技能。多個圖形進行組圖的展示,可以讓我們的結(jié)果可以多角度的展示出來,也可以進行結(jié)果差異比對的需求。 當(dāng)然,我們平時用的最多的就是PS,AI軟件處理,但是軟件對圖片中大小,位置,布局,文字等調(diào)整麻煩的很,也不是一個小工程。小果在這里教大家一個其他的方法,利用R包ggpubr進行組圖的合并,或許比AI,PS更容易呢,小果從0開始給大家介紹,我們開始學(xué)習(xí)吧!

小果在這里教學(xué)用的數(shù)據(jù)都是來自R包中自帶的數(shù)據(jù)集: 首先我們載入R包還有數(shù)據(jù)集: #install.packages("ggpubr")#這里注意的是 我們要首先載入ggplot2,在載入ggpubr包 library(ggplot2) library(ggpubr) # ToothGrowth數(shù)據(jù)集 data("ToothGrowth") head(ToothGrowth)

接下來是# mtcars 數(shù)據(jù)集 data("mtcars") mtcars$name <- rownames(mtcars) mtcars$cyl <- as.factor(mtcars$cyl) head(mtcars[, c("name", "wt", "mpg", "cyl")])

我們主要學(xué)習(xí)的是組圖,小伙伴對數(shù)據(jù)出的子圖可以更具自己需求來完成。 我們先創(chuàng)建單個的圖片 首先是箱線圖: Box_plot <- ggboxplot(ToothGrowth, x = "dose", y = "len",color = "dose", palette = "jco") Box_plot

接下里是點圖#點圖 Dot_plot <- ggdotplot(ToothGrowth, x = "dose", y = "len", ??????????????????????color = "dose", palette = "jco", binwidth = 1) Dot_plot

然后是#有序條形圖 Bar_plot <- ggbarplot(mtcars, x = "name", y = "mpg", ??????????????????????fill = "cyl",??????????????# change fill color by cyl ??????????????????????color = "white",???????????# Set bar border colors to white ??????????????????????palette = "jco",???????????# jco journal color palett. see ?ggpar ??????????????????????sort.val = "asc",??????????# Sort the value in ascending order ??????????????????????sort.by.groups = TRUE,?????# Sort inside each group ??????????????????????x.text.angle = 90??????????# Rotate vertically x axis texts ) + font("x.text", size = 8) Bar_plot

后面就是#散點圖 Scatter_plots <- ggscatter(mtcars, x = "wt", y = "mpg", ???????????????????????????add = "reg.line",??????????????# Add regression line ???????????????????????????conf.int = TRUE,???????????????# Add confidence interval ???????????????????????????color = "cyl", palette = "jco", # Color by groups "cyl" ???????????????????????????shape = "cyl"??????????????????# Change point shape by groups "cyl" )+ ??stat_cor(aes(color = cyl), label.x = 3)??????# Add correlation coefficient Scatter_plots

上述的單圖是不是都很熟悉,都是平時我們做的比較多的圖,我們創(chuàng)建完成后,就開始繪制組合圖片把 這里使用ggpubr包中函數(shù)ggarrange()在一頁上進行展示上述的結(jié)果 對ToothGrowth數(shù)據(jù)集的箱線圖,點圖組合展示: ggarrange(Box_plot, Dot_plot,labels = c("A", "B"),ncol = 2, nrow = 1)

是不是就完成了呢,AB序號小伙伴可以自行調(diào)整 后面我們對#mtcars 數(shù)據(jù)集的條形圖,散點圖組合展示 ? figure <- ggarrange(Scatter_plots, Bar_plot + font("x.text", size = 10),ncol = 1, nrow = 2) ????????????????????#添加圖形的注釋信息(標(biāo)題,副標(biāo)題,坐標(biāo)軸,字體,顏色等) ???????????????????? ????????????????????annotate_figure(figure, ????????????????????????????????????top = text_grob("Visualizing mpg", color = "red", face = "bold", size = 14), ????????????????????????????????????bottom = text_grob("Data source:?mtcars data set", color = "blue", ???????????????????????????????????????????????????????hjust = 1, x = 1, face = "italic", size = 10), ????????????????????????????????????left = text_grob("Figure arranged using ggpubr", color = "green", rot = 90), ????????????????????????????????????right = "Here )!", ????????????????????????????????????fig.lab = "Figure 1", fig.lab.face = "bold" ????????????????????)

這是組圖的一個,我們還可以調(diào)整細(xì)節(jié),不只是簡簡單單的拼圖, 接下來我們使用ggarrange()函數(shù)更改繪圖的列/行的跨度 這里我們將#散點圖在第一行跨兩列,箱形圖和點圖并于第二行 ???????????????????? ggarrange(Scatter_plots,????????????????????????????????????????????????# First row with scatter plot ??????????ggarrange(Box_plot, Dot_plot, ncol = 2, labels = c("B", "C")), # Second row with box and dot plots ??????????nrow = 2, ??????????labels = "A"???????????????????????????????????????# Labels of the scatter plot ????????????????????)

這樣一來,一行中平均排行好,圖片更加美觀 但是有時候圖片內(nèi)容多了,會顯得很擁擠,我們可以利用NULL構(gòu)建空白圖 我們這里示例一下邊際密度圖的散點圖,去學(xué)習(xí)一下吧: #繪制主要散點圖 ? Scatter_plots <- ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width", ???????????????????????????color = "Species", palette = "jco", ???????????????????????????size = 3, alpha = 0.6)+ ??border() #上側(cè),右側(cè)添加密度圖????????????????????????????????? ? xplot <- ggdensity(iris, "Sepal.Length", fill = "Species", ???????????????????palette = "jco") yplot <- ggdensity(iris, "Sepal.Width", fill = "Species", ???????????????????palette = "jco")+ ??rotate() # 設(shè)置主題 yplot <- yplot + clean_theme() xplot <- xplot + clean_theme() ? # 通過width和height參數(shù)調(diào)整圖的大小 # 利用NULL設(shè)置空白圖 ? ggarrange(xplot, NULL, Scatter_plots, yplot, ??????????ncol = 2, nrow = 2,?align = "hv", ??????????widths = c(2, 1), heights = c(1, 2), ??????????common.legend = TRUE)?

這樣邊際圖周圍留出來一些空白,我們可以將NULL套用在自己數(shù)據(jù)圖中。 當(dāng)然我們還可以添加統(tǒng)計的圖表還有文本的信息,我們可以利用繪制變量“Sepal.Length” 的密度圖以及描述性統(tǒng)計(mean,sd,...)的匯總表 # Sepal.Length密度圖 ? density.p <- ggdensity(iris, x = "Sepal.Length", ???????????????????????fill = "Species", palette = "jco") # Sepal.Length描述性統(tǒng)計 ? stable <- desc_statby(iris, measure.var = "Sepal.Length", ??????????????????????grps = "Species") stable <- stable[, c("Species", "length", "mean", "sd")] ? ? # 設(shè)置table的主題 ? stable.p <- ggtexttable(stable, rows = NULL, ????????????????????????theme = ttheme("mOrange")) #?text 信息 ? text <- paste("iris data set gives the measurements in cm", ??????????????"of the variables sepal length and width", ??????????????"and petal length and width, reScatter_plotsectively,", ??????????????"for 50 flowers from each of 3 Scatter_plotsecies of iris.", ??????????????"The Scatter_plotsecies are Iris setosa, versicolor, and virginica.", sep = " ") text.p <- ggparagraph(text = text, face = "italic", size = 11, color = "black") ? ? # 組圖展示,調(diào)整高度和寬度 ? ggarrange(density.p, stable.p, text.p, ??????????ncol = 1, nrow = 3, ??????????heights = c(1, 0.5, 0.3))

這樣一來,下面就是對上述圖的統(tǒng)計介紹,我們組合在一張圖中。 我們在調(diào)整一下布局:進行#子母圖展示 density.p + annotation_custom(ggplotGrob(stable.p), ??????????????????????????????xmin = 5.5, ymin = 0.7, ??????????????????????????????xmax = 8) ? #嵌套布局展示 ? p1 <- ggarrange(Scatter_plots, Bar_plot + font("x.text", size = 9), ????????????????ncol = 1, nrow = 2) p2 <- ggarrange(density.p, stable.p, text.p, ????????????????ncol = 1, nrow = 3, ????????????????heights = c(1, 0.5, 0.3)) #先組合P1,P2,然后自定義行 列 ,嵌套組合展示 ggarrange(p1, p2, ncol = 2, nrow = 1)

這樣是不是就大功告成了,小伙伴有沒有心動呢,快去試試吧,不要忘記多多理解代碼的意義,這樣才能套用自己數(shù)據(jù)進行展示。

手把手教學(xué)-使用ggpubr進行文章的組圖合并!?。〉脑u論 (共 條)

分享到微博請遵守國家法律
崇左市| 金寨县| 西畴县| 永清县| 松滋市| 信丰县| 宁国市| 博兴县| 越西县| 大埔区| 贺兰县| 连州市| 合阳县| 于田县| 旺苍县| 清涧县| 台北市| 麦盖提县| 龙陵县| 遵化市| 新疆| 梓潼县| 石嘴山市| 祁东县| 通城县| 公安县| 镇巴县| 宜君县| 江孜县| 会同县| 龙南县| 乌鲁木齐市| 菏泽市| 平顶山市| 唐河县| 双鸭山市| 祁东县| 遂平县| 寻乌县| 句容市| 荥阳市|