R语言 创建一维散点图 – stripchart() 函数
带状图被定义为一维散点图或点状图,当样本量较小时,它被用作箱形图的替代品。它是用 R语言中的 stripchart() 函数 创建的 。
R – stripchart函数
语法: stripchart(x, data = NULL method, jitter )
参数
- x: 要生成图表的数据值。
- data :一个数据框架,x值中的变量应从该框架中提取。
- method: 该方法主要用于分离重合的点。
- jitter: 当使用method = “jitter “时,jitter将产生应用的抖动量。
R编程语言中的带状图功能
这个例子使用了ToothGrowth数据库, stripchart()函数 是用ToothGrowth数据库实现的。
数据集
ToothGrowthdose <- as.factor(ToothGrowthdose)
# Print the first 7 rows
head(ToothGrowth, 7)
输出
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编程中的简单条形图
# Plot len by dose
stripchart(len ~ dose, data = ToothGrowth,
pch = 22, frame = FALSE)
输出
例2:用R编程语言绘制垂直条形图
# Vertical plot using method = "jitter"
stripchart(len ~ dose, data = ToothGrowth,
pch = 16, frame = FALSE, vertical = TRUE,
method = "jitter")
输出
例3:各组点形状(pch)和颜色的变化
# 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:多个数字矢量的条形图
在这个例子中,我们将使用一个虹膜数据集和包含变量的列表,并绘制条形图图谱。
# create list of variables
x <- list('Petal Length' = irisPetal.Length,
'Petal Width' = irisPetal.Width)
# create plot that contains
# one strip chart per variable
stripchart(x,
main = 'Petal Width & Petal Distributions',
col = c('Green', 'coral2'),
method = 'jitter')
输出