R ggplot2 – 边缘图

R ggplot2 – 边缘图

边际图是一种散点图,它在X轴和Y轴的边际上有直方图、boxplots或点阵图。它允许研究2个数字变量之间的关系。基准图可视化了x轴和y轴变量之间的相关性。它通常是一个散点图或一个密度图。边缘图通常绘制在基础图的顶部和右侧边缘,它们使用直方图、柱状图或密度图来显示x和y轴变量的分布。这有助于我们直观地看到两个轴上的变量在不同数值下的分布强度。

为了在R语言中绘制边际图,我们将使用R语言的ggExtra包。ggExtra是一个函数和层的集合,用于增强ggplot2。ggMarginal()函数可以用来为ggplot2散点图添加边际直方图/博列表/密度图。

安装

要安装ggExtra包,我们使用。

install.packages("ggExtra")

边际图的创建

为了创建边际图,我们使用以下函数来制作带有散点图的边际柱状图。

语法: ggMarginal( plot, type )

参数

  • plot: 确定要添加边际图的基础散点图。
  • type: 决定边际图的类型,即直方图、博列表和密度图。

例子: 用ggExtra软件包的grid.arrange()函数将基本散点图与边际直方图、密度图和箱形图全部排列在一个页面上。

# load library tidyverse, gridExtra and ggExtra
library(tidyverse)
library(ggExtra)
library(gridExtra)
 
# set theme
theme_set(theme_bw(12))
 
# create x and y vector
xAxis <- rnorm(1000)                
yAxis <- rnorm(1000) + xAxis + 10 
 
# create sample data frame
sample_data <- data.frame(xAxis, yAxis)
 
# create scatter plot using ggplot() function
plot <- ggplot(sample_data, aes(x=xAxis, y=yAxis))+
          geom_point()+
        theme(legend.position="none")
 
# use ggMarginal function to create
# marginal histogram, boxplot and density plot
plot1 <- ggMarginal(plot, type="histogram")
plot2 <- ggMarginal(plot, type="boxplot")
plot3 <- ggMarginal(plot, type="density")
 
# combine plots in a grid
grid.arrange( plot1, plot2, plot3, ncol=3)

输出

R ggplot2 - 边缘图

颜色和尺寸的定制

我们可以自定义ggmarginal()函数的参数,为我们的边际图创建所需的外观。我们可以使用ggmarginal()函数的尺寸、填充和颜色参数来分别改变边际图的相对尺寸、背景填充颜色和常规颜色。

语法: ggMarginal( plot, type, fill, color, size )

参数

  • plot: 确定要添加边际图的基础散点图。
  • type: 确定边际图的类型,即直方图、博列表和密度图。
  • fill: 确定边际图的背景填充颜色。
  • color :决定边际图的轮廓颜色
  • size : 决定绘图元素的相对大小。

例子: 在这里,我们用自定义的颜色和尺寸参数修改了上述例子中的绘图。

# load library tidyverse, gridExtra and ggExtra
library(tidyverse)
library(ggExtra)
library(gridExtra)
 
# set theme
theme_set(theme_bw(12))
 
# create x and y vector
xAxis <- rnorm(1000)                
yAxis <- rnorm(1000) + xAxis + 10 
 
# create sample data frame
sample_data <- data.frame(xAxis, yAxis)
 
# create scatter plot using ggplot() function
plot <- ggplot(sample_data, aes(x=xAxis, y=yAxis))+
          geom_point()+
        theme(legend.position="none")
 
# use ggMarginal function to create
# marginal histogram, boxplot and density plot
# fill, colo and size property is used for customization
plot1 <- ggMarginal(plot, type="histogram", fill= "green", size=10)
plot2 <- ggMarginal(plot, type="boxplot", color="yellow" )
plot3 <- ggMarginal(plot, type="density", color="green")
 
# combine plots in a grid
grid.arrange( plot1, plot2, plot3, ncol=3)

输出

R ggplot2 - 边缘图

只有一个轴的边际图

有时我们只需要一个轴的边际图,要么是x轴,要么是y轴。在这种情况下,我们使用 ggmarginal() 函数的 margins 参数。我们希望边际图出现的轴是以参数 margins 的值给出的。

语法: ggMarginal( plot, type, margins )

参数

  • plot: 确定要添加边际图的基础散点图。
  • type: 确定边际图的类型,如直方图、博列表和密度图。
  • margins: 确定需要边际图的轴。

例子: 这里有两张图,一张是X轴上的边际图,另一张是Y轴。

# load library tidyverse, gridExtra and ggExtra
library(tidyverse)
library(ggExtra)
library(gridExtra)
 
# set theme
theme_set(theme_bw(12))
 
# create x and y vector
xAxis <- rnorm(1000)                
yAxis <- rnorm(1000) + xAxis + 10 
 
# create sample data frame
sample_data <- data.frame(xAxis, yAxis)
 
# create scatter plot using ggplot() function
plot <- ggplot(sample_data, aes(x=xAxis, y=yAxis))+
          geom_point()+
        theme(legend.position="none")
 
# use ggMarginal function to create
# marginal histogram on x-axis
plot1 <- ggMarginal(plot, type="histogram", margins='x')
 
# use ggMarginal function to
# create marginal histogram on y-axis
plot2 <- ggMarginal(plot, type="histogram", margins='y')
 
# combine plots in a grid
grid.arrange( plot1, plot2, ncol=2)

输出

R ggplot2 - 边缘图

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程