gpt4 book ai didi

r - 如何将 abline() 添加到 pareto.chart()/barplot()?

转载 作者:行者123 更新时间:2023-12-04 10:56:27 27 4
gpt4 key购买 nike

enter image description here

我正在使用帕累托图(库qcc):

## abcd is a data frame, and I am producing chart for column products
Product <- abcd$products
names(Product)<-abcd$customerid
pareto.chart(Product, ylab = "Number of Products", xlab="Customer", xaxt="n")
abline(v = 1000)

由于我的数据很大,我想在 x 轴 10% 的间隔后添加 abline,但不知何故我没有得到线条。

请告诉我某些函数是否可以在这里工作或者在帕累托中不允许abline

最佳答案

帕累托图和条形图

pareto.chart() 是基于 barplot() 的,因此只要知道如何将 abline() 添加到 barplot(),您知道如何使用 pareto.chart()。为了证明它们的关系,请考虑以下事项:

## example from ?pareto.chart
defect <- c(80, 27, 66, 94, 33)
names(defect) <- c("price code", "schedule date", "supplier code", "contact num.", "part num.")
y <- pareto.chart(defect, ylab = "Error frequency")
barplot(0.2 * defect, add = TRUE, col = "grey")

pareto and bar

现在您可以看到条形图重合了。

酒吧在哪里?

pareto.chart() 不返回这些柱的位置是一个陷阱。之前我们将 pareto.chart() 的结果保存在 y 中,现在这就是 y 的全部内容:

> str(y)
num [1:5, 1:4] 94 80 66 33 27 94 174 240 273 300 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:5] "contact num." "price code" "supplier code" "part num." ...
..$ Pareto chart analysis for defect: chr [1:4] "Frequency" "Cum.Freq." "Percentage" "Cum.Percent."

这就是要打印的全部内容:

> y               

Pareto chart analysis for defect
Frequency Cum.Freq. Percentage Cum.Percent.
contact num. 94 94 31.33333 31.33333
price code 80 174 26.66667 58.00000
supplier code 66 240 22.00000 80.00000
part num. 33 273 11.00000 91.00000
schedule date 27 300 9.00000 100.00000

这样,我们必须调用 barplot() 才能获取柱形位置:

x <- as.numeric(barplot(defect, plot = FALSE))
# > x
# [1] 0.7 1.9 3.1 4.3 5.5

现在,如果我们在这些位置上执行 abline():

pareto.chart(defect, ylab = "Error frequency")
abline(v = x, col = 2) ## red

abline

每 0.1 分位数添加 abline()/segments()

我建议使用:

x <- range(as.numeric(barplot(Product, plot = FALSE)))
x0 <- seq(x[1], x[2], length = 11) ## 11 break points for 10 intervals
y <- pareto.chart(Product, ylab = "Number of Products", xlab="Customer", xaxt="n")[, 2]
y0 <- y[round(seq(from = 1, to = length(y), length = 11))]
## abline(v = v0, col = "purple")
segments(x0, rep(0, 11), x0, y0, col = "purple")

作为演示,我使用

set.seed(0); Product <- rbinom(100, 20, 0.3)

plot

关于r - 如何将 abline() 添加到 pareto.chart()/barplot()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37915779/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com