最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

主要使用ggExtra結(jié)合ggplot2兩個R包進行繪制。(勝在簡潔方便)使用cowplotggpubr進行繪制。(勝在靈活且美觀)

下面的繪圖我們均以iris數(shù)據(jù)集為例。

1. 使用ggExtra結(jié)合ggplot2

1)傳統(tǒng)散點圖

# 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)整點的透明度;shape 調(diào)整點的形狀
  theme_bw() +
  theme(legend.position = "bottom") + # 圖例置于底部
  labs(x = "Sepal Length", y = "Sepal Width") # 添加x,y軸的名稱
p

下面我們一行代碼添加邊際分布(分別以密度曲線與直方圖的形式來展現(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)箱線圖(寬窄的顯示會有些問題)

# marginal plot: boxplot
ggMarginal(p, type = "boxplot", groupColour = TRUE, groupFill = TRUE)

5)小提琴圖(會有重疊,不建議使用)

# marginal plot: violin
ggMarginal(p, type = "violin", groupColour = TRUE, groupFill = TRUE)

6)密度函數(shù)與直方圖同時展現(xiàn)

# marginal plot: densigram
ggMarginal(p, type = "densigram", groupColour = TRUE, groupFill = TRUE)

2. 使用cowplot與ggpubr

1)重繪另一種散點圖

# 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)無縫拼接

# 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語言繪制散點圖結(jié)合邊際分布圖教程的詳細內(nèi)容,更多關(guān)于R語言繪制散點圖結(jié)合邊際分布圖的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • R語言兩組變量特征相關(guān)關(guān)系熱圖繪制畫法

    R語言兩組變量特征相關(guān)關(guān)系熱圖繪制畫法

    本文為大家介紹了如何畫兩組變量(特征)的相關(guān)關(guān)系熱圖的方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2022-02-02
  • R語言中g(shù)gplot2繪制雙坐標軸圖

    R語言中g(shù)gplot2繪制雙坐標軸圖

    本文主要介紹了R語言中g(shù)gplot2繪制雙坐標軸圖,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • R語言strsplit函數(shù)用法深入詳解

    R語言strsplit函數(shù)用法深入詳解

    這篇文章主要介紹了R語言strsplit函數(shù)用法深入詳解,代碼實例講解的很清晰,有感興趣的同學可以研究下
    2021-03-03
  • 用R語言繪制函數(shù)曲線圖

    用R語言繪制函數(shù)曲線圖

    這篇文章主要介紹了如何用R語言繪制函數(shù)曲線圖,幫助大家更好的理解和學習使用R語言,感興趣的朋友可以了解下
    2021-03-03
  • R語言讀取excel數(shù)據(jù)的方法(兩行命令)

    R語言讀取excel數(shù)據(jù)的方法(兩行命令)

    這篇文章主要介紹了R語言讀取excel數(shù)據(jù)的方法(兩行命令),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • R語言如何進行線性回歸的擬合度詳解

    R語言如何進行線性回歸的擬合度詳解

    這篇文章主要給大家介紹了關(guān)于R語言如何進行線性回歸的擬合度的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • R語言:排序的應(yīng)用操作

    R語言:排序的應(yīng)用操作

    這篇文章主要介紹了R語言:排序的應(yīng)用操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • R語言數(shù)據(jù)可視化繪制Circular?bar?plot實現(xiàn)環(huán)形柱狀圖

    R語言數(shù)據(jù)可視化繪制Circular?bar?plot實現(xiàn)環(huán)形柱狀圖

    這篇文章主要為大家介紹了R語言繪制Circular?bar?plot實現(xiàn)環(huán)形柱狀圖的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2022-02-02
  • R語言日期時間的使用

    R語言日期時間的使用

    日期時間是常用的一種類型,本文主要介紹了R語言日期時間的使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • R語言實現(xiàn)各種數(shù)據(jù)可視化的超詳細教程

    R語言實現(xiàn)各種數(shù)據(jù)可視化的超詳細教程

    Python語言越來越流行,尤其是在機器學習與深度學習等領(lǐng)域,但是R語言在數(shù)據(jù)分析與可視化方面仍然具有絕對的優(yōu)勢,下面這篇文章主要給大家介紹了關(guān)于R語言實現(xiàn)各種數(shù)據(jù)可視化的超詳細教程,需要的朋友可以參考下
    2022-11-11

最新評論

庆安县| 广汉市| 玉田县| 应城市| 江城| 安福县| 潼南县| 新宾| 定远县| 舒兰市| 南靖县| 雷州市| 德化县| 抚顺市| 巴青县| 澄迈县| 赤壁市| 新余市| 军事| 财经| 南江县| 鱼台县| 葵青区| 嘉祥县| 昭通市| 南召县| 康定县| 秀山| 宁德市| 昭平县| 长治县| 理塘县| 河北区| 丰镇市| 资源县| 思茅市| 乳源| 鄯善县| 观塘区| 山东| 左贡县|