R语言 从ggplot2面状图中删除标签
在这篇文章中,我们将讨论如何在R编程语言中的ggplot2中删除面图的标签。
切面图,是指人们根据一个分类变量对数据进行细分,并以相同的比例绘制一系列类似的图。我们可以使用ggplot2包中的 facet_wrap() 函数轻松地绘制一个分面图。当我们在ggplot2中使用facet_wrap()时,默认情况下,它会根据每张图所划分的组别给它们一个标题。
语法
plot + facet_wrap( ~facet-variable)
参数
- facet-variable: 决定了必须围绕着哪个变量来绘制图表。
这里,是一个使用R语言原生提供的钻石数据框架绘制的基本面图。我们使用了facet_wrap()函数和~clarity函数,将图表根据其清晰度划分成不同的面。
例子: 基本图
# load library ggridges and tidyverse
library(ggridges)
library(tidyverse)
# Basic facet plot divided according to category cut
# ggplot() function is used to plot the chart
ggplot(diamonds, aes(x=price, y=color, fill=color)) +
# geom_density_ridges() function is used to draw
# ridgeline plot
geom_density_ridges()+
# facet_wrap() function divides the plot in facets
# according to category of cut
facet_wrap(~cut)
输出
从Facet图中删除标签
我们可以使用theme()函数来定制ggplot2的各个方面。要想从facet plot中移除标签,我们需要在theme()层中使用 “strip.text.x “参数,参数为’element_blank()’。
语法
plot + theme( strip.text.x = element_blank() )
例子: 从面图中删除标签
# load library ggridges and tidyverse
library(ggridges)
library(tidyverse)
# Basic facet plot divided according to category cut
# ggplot() function is used to plot the chart
ggplot(diamonds, aes(x=price, y=color, fill=color)) +
# geom_density_ridges() function is used to draw
# ridgeline plot
geom_density_ridges()+
# facet_wrap() function divides the plot
# in facets according to category of cut
facet_wrap(~cut)+
# strip.text.x parameter of theme
# function is used to remove the label of facet plot
# element_blank() removes the label
theme(strip.text.x = element_blank())
输出