📜  如何在 R 中的 ggplot2 中自定义构面图中的边框

📅  最后修改于: 2022-05-13 01:54:22.201000             🧑  作者: Mango

如何在 R 中的 ggplot2 中自定义构面图中的边框

在本文中,我们将讨论如何在 R 编程语言的 ggplot2 中自定义分面图中的边框。

刻面图,其中一个基于分类变量对数据进行子集化,并制作一系列具有相同比例的相似图。分面有助于我们展示两个以上数据类别之间的关系。当我们有多个变量时,可以通过分面将其绘制在单个图中,绘制成更小的图。

我们可以使用 ggplot2 包的 facet_wrap()函数轻松绘制多面图。当我们在 ggplot2 中使用 facet_wrap() 时,默认情况下它会在灰色框中给出一个标题。

创建一个基本的分面图

这里,是使用 R 语言原生提供的 diamonds 数据框制作的基本面图。我们使用带有 ~cut 的 facet_wrap()函数根据其清晰度将图划分为多个方面。

R
# load library tidyverse
library(tidyverse)
  
# set theme_bw()
theme_set(theme_bw(18))
  
# Basic facet plot divided according to category 
# clarity diamonds data frame is used in plot which
# is provided natively by R Language
# ggplot() function is used to plot the chart
ggplot(diamonds, aes(x="price")) +
  
# geom_bar function is used to create bar plot
  geom_bar()+
  
# facet_wrap() function divides the plot in
# facets according to category of clarity
  facet_wrap(~clarity)


R
# load library tidyverse
library(tidyverse)
  
# set theme_bw()
theme_set(theme_bw(18))
  
# Basic facet plot divided according to category clarity
# diamonds data frame is used in plot which
# is provided natively by R Language
# ggplot() function is used to plot the chart
ggplot(diamonds, aes(x="price")) +
  
# geom_bar function is used to create bar plot
  geom_bar()+
  
# facet_wrap() function divides the plot in
# facets according to category of clarity
  facet_wrap(~clarity)+
  
# theme function with panel.spacing argument
# is used to modify space between panels
theme( panel.spacing.x = unit(0,"line"), 
       panel.spacing.y = unit(0,"line"))


R
# load library tidyverse
library(tidyverse)
  
# set theme_bw()
theme_set(theme_bw(18))
  
# Basic facet plot divided according to category clarity
# diamonds data frame is used in plot which
# is provided natively by R Language
# ggplot() function is used to plot the chart
ggplot(diamonds, aes(x="price")) +
  
# geom_bar function is used to create bar plot
  geom_bar()+
  
# facet_wrap() function divides the plot in
# facets according to category of clarity
  facet_wrap(~clarity)+
  
# theme function with panel.border is
# used to remove border of facet panels
theme( panel.border = element_blank() )


输出:

删除构面图中面板之间的空间

要删除面板之间的空间,我们使用 theme() 函数的 panel.spacing.x / panel.spacing.y 参数。我们甚至可以使用这种方法手动指定面板之间所需的空间量。

例子:

在这里,我们使用 diamonds 数据集创建了一个多面条形图,并通过使用 theme() 函数的 panel.spacing.x 和 panel.spacing.y 参数删除了面板之间的空间。

R

# load library tidyverse
library(tidyverse)
  
# set theme_bw()
theme_set(theme_bw(18))
  
# Basic facet plot divided according to category clarity
# diamonds data frame is used in plot which
# is provided natively by R Language
# ggplot() function is used to plot the chart
ggplot(diamonds, aes(x="price")) +
  
# geom_bar function is used to create bar plot
  geom_bar()+
  
# facet_wrap() function divides the plot in
# facets according to category of clarity
  facet_wrap(~clarity)+
  
# theme function with panel.spacing argument
# is used to modify space between panels
theme( panel.spacing.x = unit(0,"line"), 
       panel.spacing.y = unit(0,"line"))

输出:

删除构面图中的面板边界线

要删除面板边框线,我们使用 theme()函数的 panel.border 参数。我们使用 element.blank()函数作为 panel.border 参数的参数来移除边框。

例子:

在这里,我们使用 Diamonds 数据集创建了一个多面条形图,并通过在 theme()函数中使用 panel.border aselement_blank() 删除了面板边框。

R

# load library tidyverse
library(tidyverse)
  
# set theme_bw()
theme_set(theme_bw(18))
  
# Basic facet plot divided according to category clarity
# diamonds data frame is used in plot which
# is provided natively by R Language
# ggplot() function is used to plot the chart
ggplot(diamonds, aes(x="price")) +
  
# geom_bar function is used to create bar plot
  geom_bar()+
  
# facet_wrap() function divides the plot in
# facets according to category of clarity
  facet_wrap(~clarity)+
  
# theme function with panel.border is
# used to remove border of facet panels
theme( panel.border = element_blank() )

输出: