在 R 中更改 Boxplot 的轴标签
箱形图是一种图表,用于通过为每个图表绘制箱线图以分布的形式显示信息。箱线图帮助我们按四分位数可视化数据的分布并检测异常值的存在。为箱线图添加轴标签将有助于箱线图的可读性。
在本文中,我们将讨论如何在 R 编程语言中更改 boxplot 的轴标签。
方法 1:使用基础 R
Boxplots 是通过使用boxplot()函数在 R 编程语言中创建的。
Syntax:
boxplot(x, data, notch, varwidth, names, main)
Parameters:
- x: This parameter sets as a vector or a formula.
- data: This parameter sets the data frame.
- notch: This parameter is the label for horizontal axis.
- varwidth: This parameter is a logical value. Set as true to draw width of the box proportionate to the sample size.
- main: This parameter is the title of the chart.
- names: This parameter are the group labels that will be showed under each boxplot.
如果使用基本 R 制作,我们使用boxplot()函数的名称参数。对于此箱线图数据,必须首先初始化并且必须添加到轴的名称作为向量传递。然后调用 boxplot() 并将 data 和 names 参数设置为此向量。
例子:
R
# sample data for plotting
geeksforgeeks=c(120,26,39,49,15)
scripter=c(115,34,30,92,81)
writer=c(100,20,15,32,23)
# labels for Axis
label=c("geeksforgeeks","scripter","writer")
# boxplot with names parameter for labels
boxplot(geeksforgeeks, scripter, writer, names=label)
R
# sample data for plotting
geeksforgeeks=c(120,26,39,49,15)
scripter=c(115,34,30,92,81)
writer=c(100,20,15,32,23)
# labels for Axis
label=c("geeksforgeeks","scripter","writer")
# boxplot with names parameter for labels
boxplot(geeksforgeeks, scripter, writer,
names=label, horizontal=TRUE)
R
# load package reshape2 and ggplot2
library("reshape2")
library("ggplot2")
# Create sample data
set.seed(97364)
sample <- data.frame(x1 = rnorm(200),
x2 = rnorm(200, 2),
x3 = rnorm(200, 3, 3))
# Reshape sample data to long form
sample_main <- melt(sample)
# Add variable parameter for axis label in dataset
levels(sample_main$variable) <- c("geeksforgeeks","scripter","writer")
# Draw boxplot
ggplot(sample_main, aes(variable, value)) +
geom_boxplot()
R
# load package reshape2 and ggplot2
library("reshape2")
library("ggplot2")
# Create sample data
set.seed(97364)
sample <- data.frame(x1 = rnorm(200),
x2 = rnorm(200, 2),
x3 = rnorm(200, 3, 3))
# Reshape sample data to long form
sample_main <- melt(sample)
# Add variable parameter for axis label in dataset
levels(sample_main$variable) <- c("geeksforgeeks","scripter","writer")
# Draw boxplot
ggplot(sample_main, aes(variable, value)) +
geom_boxplot() + coord_flip()
输出:
这也可以很容易地对水平箱线图完成。要将其转换为水平箱线图,请添加参数Horizontal=True ,其余任务保持不变。为此,标签将出现在 y 轴上。
例子:
电阻
# sample data for plotting
geeksforgeeks=c(120,26,39,49,15)
scripter=c(115,34,30,92,81)
writer=c(100,20,15,32,23)
# labels for Axis
label=c("geeksforgeeks","scripter","writer")
# boxplot with names parameter for labels
boxplot(geeksforgeeks, scripter, writer,
names=label, horizontal=TRUE)
输出:
方法二:使用ggplot2
如果使用ggplot2 制作,我们会在绘制箱线图之前更改数据集中的标签数据。
Reshape 模块用于将样本数据从宽格式转换为长格式,ggplot2 将用于绘制箱线图。创建数据后,使用melt函数将数据从宽格式转换为长格式。现在,更改数据集中的变量名称并简单地绘制箱线图。
例子:
电阻
# load package reshape2 and ggplot2
library("reshape2")
library("ggplot2")
# Create sample data
set.seed(97364)
sample <- data.frame(x1 = rnorm(200),
x2 = rnorm(200, 2),
x3 = rnorm(200, 3, 3))
# Reshape sample data to long form
sample_main <- melt(sample)
# Add variable parameter for axis label in dataset
levels(sample_main$variable) <- c("geeksforgeeks","scripter","writer")
# Draw boxplot
ggplot(sample_main, aes(variable, value)) +
geom_boxplot()
输出:
这可以很容易地对水平箱线图完成。要将其转换为水平箱线图,请在箱线图代码中添加coord_flip() ,其余部分与上述相同。
句法:
geom_boxplot() + coord_flip()
例子:
电阻
# load package reshape2 and ggplot2
library("reshape2")
library("ggplot2")
# Create sample data
set.seed(97364)
sample <- data.frame(x1 = rnorm(200),
x2 = rnorm(200, 2),
x3 = rnorm(200, 3, 3))
# Reshape sample data to long form
sample_main <- melt(sample)
# Add variable parameter for axis label in dataset
levels(sample_main$variable) <- c("geeksforgeeks","scripter","writer")
# Draw boxplot
ggplot(sample_main, aes(variable, value)) +
geom_boxplot() + coord_flip()
输出: