亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用R語(yǔ)言繪制散點(diǎn)圖結(jié)合邊際分布圖教程

 更新時(shí)間:2021年11月05日 11:54:56   作者:Kanny廣小隸  
這篇文章主要介紹了使用R語(yǔ)言利用ggplot繪制散點(diǎn)圖,并且在圖像的兩邊繪制邊際分布圖(包括邊際直方圖與邊際密度函數(shù))我們這里介紹兩種方法進(jìn)行繪制

主要使用ggExtra結(jié)合ggplot2兩個(gè)R包進(jìn)行繪制。(勝在簡(jiǎn)潔方便)使用cowplotggpubr進(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)

參考

Articles - ggpubr: Publication Ready Plots——Perfect Scatter Plots with Correlation and Marginal Histograms

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包ggtreeExtra繪制進(jìn)化樹(shù)

    R包ggtreeExtra繪制進(jìn)化樹(shù)

    這篇文章主要為大家介紹了R包ggtreeExtra繪制進(jìn)化樹(shù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • R語(yǔ)言作圖之直方圖histogram繪制過(guò)程詳解

    R語(yǔ)言作圖之直方圖histogram繪制過(guò)程詳解

    這篇文章主要介紹了R語(yǔ)言作圖之直方圖histogram詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • R語(yǔ)言日期時(shí)間的使用

    R語(yǔ)言日期時(shí)間的使用

    日期時(shí)間是常用的一種類型,本文主要介紹了R語(yǔ)言日期時(shí)間的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • R語(yǔ)言對(duì)CSV文件操作實(shí)例講解

    R語(yǔ)言對(duì)CSV文件操作實(shí)例講解

    在本篇文章里小編給大家整理了一篇關(guān)于R語(yǔ)言對(duì)CSV文件操作實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-04-04
  • 詳解R語(yǔ)言實(shí)現(xiàn)前向逐步回歸(前向選擇模型)

    詳解R語(yǔ)言實(shí)現(xiàn)前向逐步回歸(前向選擇模型)

    本文主要介紹了詳解R語(yǔ)言實(shí)現(xiàn)前向逐步回歸,從實(shí)現(xiàn)原理開(kāi)始,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • R語(yǔ)言數(shù)據(jù)可視化繪制Circular?bar?plot實(shí)現(xiàn)環(huán)形柱狀圖

    R語(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-02
  • R語(yǔ)言-如何將list轉(zhuǎn)換為向量

    R語(yǔ)言-如何將list轉(zhuǎn)換為向量

    這篇文章主要介紹了R語(yǔ)言-將list轉(zhuǎn)換為向量的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • R語(yǔ)言多元Logistic邏輯回歸應(yīng)用實(shí)例

    R語(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-03
  • R語(yǔ)言boxplot函數(shù)深入講解

    R語(yǔ)言boxplot函數(shù)深入講解

    這篇文章主要介紹了R語(yǔ)言boxplot函數(shù)深入講解,文中圖文講解的很透徹,有感興趣的同學(xué)可以研究下
    2021-03-03
  • R語(yǔ)言入門使用RStudio制作包含Rcpp代碼的R包

    R語(yǔ)言入門使用RStudio制作包含Rcpp代碼的R包

    這篇文章主要為大家介紹了R語(yǔ)言入門使用RStudio來(lái)制作包含Rcpp代碼的R包,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-11-11

最新評(píng)論