服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - R语言 - 使用R语言绘制散点图结合边际分布图教程

使用R语言绘制散点图结合边际分布图教程

2022-01-19 13:32Kanny广小隶 R语言

这篇文章主要介绍了使用R语言利用ggplot绘制散点图,并且在图像的两边绘制边际分布图(包括边际直方图与边际密度函数)我们这里介绍两种方法进行绘制

主要使用ggExtra结合ggplot2两个R包进行绘制。(胜在简洁方便)使用cowplot与ggpubr进行绘制。(胜在灵活且美观)

下面的绘图我们均以iris数据集为例。

 

1. 使用ggExtra结合ggplot2

1)传统散点图

# 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 调整点的透明度;shape 调整点的形状
theme_bw() +
theme(legend.position = "bottom") + # 图例置于底部
labs(x = "Sepal Length", y = "Sepal Width") # 添加x,y轴的名称
p

使用R语言绘制散点图结合边际分布图教程

下面我们一行代码添加边际分布(分别以密度曲线与直方图的形式来展现):

2)密度函数

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

使用R语言绘制散点图结合边际分布图教程

3)直方图

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

使用R语言绘制散点图结合边际分布图教程

4)箱线图(宽窄的显示会有些问题)

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

使用R语言绘制散点图结合边际分布图教程

5)小提琴图(会有重叠,不建议使用)

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

使用R语言绘制散点图结合边际分布图教程

6)密度函数与直方图同时展现

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

使用R语言绘制散点图结合边际分布图教程

 

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

使用R语言绘制散点图结合边际分布图教程

2)有缝拼接

① 密度函数

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))

使用R语言绘制散点图结合边际分布图教程

② 未被压缩的箱线图

# 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))

使用R语言绘制散点图结合边际分布图教程

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)

使用R语言绘制散点图结合边际分布图教程

 

参考

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

Marginal distribution with ggplot2 and ggExtra

以上就是使用R语言绘制散点图结合边际分布图教程的详细内容,更多关于R语言绘制散点图结合边际分布图的资料请关注服务器之家其它相关文章!

原文链接:https://kanny.blog.csdn.net/article/details/107088937

延伸 · 阅读

精彩推荐
  • R语言R语言中qplot()函数的用法说明

    R语言中qplot()函数的用法说明

    这篇文章主要介绍了R语言中qplot()函数的用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    Jack_丁明12752022-01-05
  • R语言如何用R语言绘制散点图

    如何用R语言绘制散点图

    这篇文章主要介绍了如何用R语言绘制散点图,帮助大家更好的理解和学习使用R语言,感兴趣的朋友可以了解下...

    菜鸟教程13002021-12-23
  • R语言R语言常量知识点总结

    R语言常量知识点总结

    在本篇文章里小编给大家整理了一篇关于R语言常量知识点总结内容,有兴趣的朋友们可以学习分享下。...

    R语言教程网12102021-12-29
  • R语言R语言中的vector(向量),array(数组)使用总结

    R语言中的vector(向量),array(数组)使用总结

    这篇文章主要介绍了R语言中的vector(向量),array(数组)使用总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    A叶子叶来5772021-11-14
  • R语言R语言实现支持向量机SVM应用案例

    R语言实现支持向量机SVM应用案例

    本文主要介绍了R语言实现支持向量机SVM应用案例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    一天_pika5222022-01-18
  • R语言基于R/RStudio中安装包“无法与服务器建立连接”的解决方案

    基于R/RStudio中安装包“无法与服务器建立连接”的解决方案

    这篇文章主要介绍了基于R/RStudio中安装包“无法与服务器建立连接”的解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    truffle52815052022-01-05
  • R语言R语言读取xls与xlsx格式文件过程

    R语言读取xls与xlsx格式文件过程

    这篇文章主要为大家介绍了使用R语言读取xls与xlsx格式文件的过程步骤,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪...

    Kanny广小隶11982022-01-20
  • R语言R语言gsub替换字符工具的具体使用

    R语言gsub替换字符工具的具体使用

    这篇文章主要介绍了R语言gsub替换字符工具的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友...

    lztttao10372021-12-24