使用R語(yǔ)言繪制散點(diǎn)圖結(jié)合邊際分布圖教程
主要使用ggExtra
結(jié)合ggplot2
兩個(gè)R包進(jìn)行繪制。(勝在簡(jiǎn)潔方便)使用cowplot
與ggpubr
進(jìn)行繪制。(勝在靈活且美觀)
下面的繪圖我們均以iris數(shù)據(jù)集為例。
1. 使用ggExtra結(jié)合ggplot2
1)傳統(tǒng)散點(diǎn)圖
# library library(ggplot2) library(ggExtra) # classic plot p <- ggplot(iris) + geom_point(aes(x = Sepal.Length, y = Sepal.Width, color = Species), alpha = 0.6, shape = 16) + # alpha 調(diào)整點(diǎn)的透明度;shape 調(diào)整點(diǎn)的形狀 theme_bw() + theme(legend.position = "bottom") + # 圖例置于底部 labs(x = "Sepal Length", y = "Sepal Width") # 添加x,y軸的名稱 p
下面我們一行代碼添加邊際分布(分別以密度曲線與直方圖的形式來(lái)展現(xiàn)):
2)密度函數(shù)
# marginal plot: density ggMarginal(p, type = "density", groupColour = TRUE, groupFill = TRUE)
3)直方圖
# marginal plot: histogram ggMarginal(p, type = "histogram", groupColour = TRUE, groupFill = TRUE)
4)箱線圖(寬窄的顯示會(huì)有些問(wèn)題)
# marginal plot: boxplot ggMarginal(p, type = "boxplot", groupColour = TRUE, groupFill = TRUE)
5)小提琴圖(會(huì)有重疊,不建議使用)
# marginal plot: violin ggMarginal(p, type = "violin", groupColour = TRUE, groupFill = TRUE)
6)密度函數(shù)與直方圖同時(shí)展現(xiàn)
# marginal plot: densigram ggMarginal(p, type = "densigram", groupColour = TRUE, groupFill = TRUE)
2. 使用cowplot與ggpubr
1)重繪另一種散點(diǎn)圖
# Scatter plot colored by groups ("Species") sp <- ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width", color = "Species", palette = "jco", size = 3, alpha = 0.6) + border() + theme(legend.position = "bottom") sp
2)有縫拼接
① 密度函數(shù)
library(cowplot) # Marginal density plot of x (top panel) and y (right panel) xplot <- ggdensity(iris, "Sepal.Length", fill = "Species", palette = "jco") yplot <- ggdensity(iris, "Sepal.Width", fill = "Species", palette = "jco") + rotate() # Cleaning the plots sp <- sp + rremove("legend") yplot <- yplot + clean_theme() + rremove("legend") xplot <- xplot + clean_theme() + rremove("legend") # Arranging the plot using cowplot plot_grid(xplot, NULL, sp, yplot, ncol = 2, align = "hv", rel_widths = c(2, 1), rel_heights = c(1, 2))
② 未被壓縮的箱線圖
# Marginal boxplot of x (top panel) and y (right panel) xplot <- ggboxplot(iris, x = "Species", y = "Sepal.Length", color = "Species", fill = "Species", palette = "jco", alpha = 0.5, ggtheme = theme_bw())+ rotate() yplot <- ggboxplot(iris, x = "Species", y = "Sepal.Width", color = "Species", fill = "Species", palette = "jco", alpha = 0.5, ggtheme = theme_bw()) # Cleaning the plots sp <- sp + rremove("legend") yplot <- yplot + clean_theme() + rremove("legend") xplot <- xplot + clean_theme() + rremove("legend") # Arranging the plot using cowplot plot_grid(xplot, NULL, sp, yplot, ncol = 2, align = "hv", rel_widths = c(2, 1), rel_heights = c(1, 2))
3)無(wú)縫拼接
# Main plot pmain <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point() + color_palette("jco") # Marginal densities along x axis xdens <- axis_canvas(pmain, axis = "x") + geom_density(data = iris, aes(x = Sepal.Length, fill = Species), alpha = 0.7, size = 0.2) + fill_palette("jco") # Marginal densities along y axis # Need to set coord_flip = TRUE, if you plan to use coord_flip() ydens <- axis_canvas(pmain, axis = "y", coord_flip = TRUE) + geom_density(data = iris, aes(x = Sepal.Width, fill = Species), alpha = 0.7, size = 0.2) + coord_flip() + fill_palette("jco") p1 <- insert_xaxis_grob(pmain, xdens, grid::unit(.2, "null"), position = "top") p2 <- insert_yaxis_grob(p1, ydens, grid::unit(.2, "null"), position = "right") ggdraw(p2)
參考
Marginal distribution with ggplot2 and ggExtra
以上就是使用R語(yǔ)言繪制散點(diǎn)圖結(jié)合邊際分布圖教程的詳細(xì)內(nèi)容,更多關(guān)于R語(yǔ)言繪制散點(diǎn)圖結(jié)合邊際分布圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
R語(yǔ)言作圖之直方圖histogram繪制過(guò)程詳解
這篇文章主要介紹了R語(yǔ)言作圖之直方圖histogram詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03詳解R語(yǔ)言實(shí)現(xiàn)前向逐步回歸(前向選擇模型)
本文主要介紹了詳解R語(yǔ)言實(shí)現(xiàn)前向逐步回歸,從實(shí)現(xiàn)原理開(kāi)始,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08R語(yǔ)言數(shù)據(jù)可視化繪制Circular?bar?plot實(shí)現(xiàn)環(huán)形柱狀圖
這篇文章主要為大家介紹了R語(yǔ)言繪制Circular?bar?plot實(shí)現(xiàn)環(huán)形柱狀圖的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02R語(yǔ)言多元Logistic邏輯回歸應(yīng)用實(shí)例
這篇文章主要給大家介紹了關(guān)于R語(yǔ)言多元Logistic邏輯回歸應(yīng)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03R語(yǔ)言入門使用RStudio制作包含Rcpp代碼的R包
這篇文章主要為大家介紹了R語(yǔ)言入門使用RStudio來(lái)制作包含Rcpp代碼的R包,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11