在 R 编程中创建一维散点图 – stripchart()函数
条形图定义为一维散点图或点图,当样本量较小时,可用作箱线图的替代方案。它是使用R 语言中的 stripchart()函数创建的。
R – 条形图函数
Syntax: stripchart(x, data = NULL method, jitter )
Parameters:
- x : value of data from which the plots are to be produced.
- data : a data.frame from which the variables in x value should be taken.
- Method : the method is basically used to separate coincident points.
- jitter : when method = “jitter” is used, jitter will produce the amount of jittering applied.
R 编程语言中的条形图函数
本示例使用 ToothGrowth 数据库, stripchart()函数是使用 ToothGrowth 数据库实现的。
数据集:
R
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
# Print the first 7 rows
head(ToothGrowth, 7)
R
# Plot len by dose
stripchart(len ~ dose, data = ToothGrowth,
pch = 22, frame = FALSE)
R
# Vertical plot using method = "jitter"
stripchart(len ~ dose, data = ToothGrowth,
pch = 16, frame = FALSE, vertical = TRUE,
method = "jitter")
R
# Change point shapes (pch) and colors by groups
# add main title and axis labels
stripchart(len ~ dose, data = ToothGrowth,
frame = FALSE, vertical = TRUE,
method = "jitter", pch = c(16, 19, 18),
col = c("blue ", "darkgreen", "orange"),
main = "Length / dose", xlab = "Dose",
ylab = "Length")
R
# create list of variables
x <- list('Petal Length' = iris$Petal.Length,
'Petal Width' = iris$Petal.Width)
# create plot that contains
# one strip chart per variable
stripchart(x,
main = 'Petal Width & Petal Distributions',
col = c('Green', 'coral2'),
method = 'jitter')
输出:
len supp dose
1 4.2 VC 0.5
2 11.5 VC 0.5
3 7.3 VC 0.5
4 5.8 VC 0.5
5 6.4 VC 0.5
6 10.0 VC 0.5
7 11.2 VC 0.5
以上代码将打印数据集
示例 1:R 编程中的简单条形图
R
# Plot len by dose
stripchart(len ~ dose, data = ToothGrowth,
pch = 22, frame = FALSE)
输出:
示例 2:在 R 编程语言中绘制垂直条形图
R
# Vertical plot using method = "jitter"
stripchart(len ~ dose, data = ToothGrowth,
pch = 16, frame = FALSE, vertical = TRUE,
method = "jitter")
输出:
示例 3:按组更改点形状 (pch) 和颜色
R
# Change point shapes (pch) and colors by groups
# add main title and axis labels
stripchart(len ~ dose, data = ToothGrowth,
frame = FALSE, vertical = TRUE,
method = "jitter", pch = c(16, 19, 18),
col = c("blue ", "darkgreen", "orange"),
main = "Length / dose", xlab = "Dose",
ylab = "Length")
输出:
上面的示例将添加主标题和轴标签并打印彩色条形图。
示例 4:多个数值向量的条形图
在此示例中,我们将使用包含变量的 iris 数据集和列表并绘制条形图。
R
# create list of variables
x <- list('Petal Length' = iris$Petal.Length,
'Petal Width' = iris$Petal.Width)
# create plot that contains
# one strip chart per variable
stripchart(x,
main = 'Petal Width & Petal Distributions',
col = c('Green', 'coral2'),
method = 'jitter')
输出: